Electronic comparator circuits have very sharp, precise transition points. While generally this is good, it can be very troublesome. In your circuit, even after the RC integration of the PWM signal there is still about a 180 milliVolts of peak-to-peak ripple. This will produce chatter on the output of the comparator when it is near the 18 Volts trip point. I suspect that you may have noticed this and that may have been why you put in C2, as that capacitance would tend to mask the problem.
The proper solution is to ad hysteresis to the comparator. What this does is that it makes the rising and falling transition points different. There is no hanging on the edge, because as soon as you get to the edge, the edge moves and you find you're already passed it.
To impliment hysteresis in your circuit, insert a 10K resistor between the top of C1 and the noninverting input of U1. Add a 1 Meg resitor from the output ot U1 to the noninverting input. These values add about 235miliVolts of hysteresis. More than enough to offset the peak-to-peak ripple on your input signal.