OP-Amp Diode Clamping Circuit
A different approach might be to use a zener to limit/clamp the maximum voltage and the op amp as a unity gain follower (buffer).

The op amp output will simply follow any input voltage below 6V8 (ie Vout = Vin) because the zener does not conduct. Under the fault condition (V &gt; 7) the zener clamps the voltage to its rated voltage. Between 6V8 and 24 V the non-inverting input 'sees' only a small change in voltage around 6V8 and the output of the op amp stays at the same level.
C1 is a small capacitor to smooth out any noise.
I'm assuming here that 7 V is an arbitary and not critical figure somewhere above the 5V but not much more. Zeners come in particular voltage/power ratings so a 6V8, 400mW type should suffice.
If you wanted to get closer to the 5V level just replace the zener with an appropriate value (e.g. 5V1)