How to prevent voltage spikes when motor is turned off
If you are using a H bridge type circuit in your ESC then the surge might be due to not having enough bulk capacitance across the 24 volt power so that the excess energy from motor deceleration or back emf (less likely) can be absorbed into the capacitor. It's common practice. See the picture below: -



In red is the bulk capacitor. The purple arrows and diodes show a route of the current when the motor is free running due to all the MOSFETs being off. If there is sufficient torque on the motor due to the load, it will attempt to back-feed the power supply and bulk capacitor. The bulk capacitor is the first line of defense in that it won't prevent a small rise in bus voltage (\$V_{DD}\$) but can overcome most difficulties in pushing rotational energy back onto the bus.

However, this may not be sufficient and a braking resistor might be considered to absorb that energy into heat. This means bulk capacitor plus braking resistor. If all else fails and the bus voltage does rise over-excessively then a zener clamp or TVS might be required.