Your fan has inductance and a magnetic field is produced in relation to the AC current through it. When you interrupt the current (turn the fan off) the magnetic field will collapse and that will produce a transient voltage. (depending on where in the cycle the switch is opened will have a relation to how large the spike can be)
Cheap power supplies do not provide much protection from these spikes appearing on the output dc. Getting rid of these disturbances is not always easy.
Adding additional capacitance to the input of your circuit might help. But what would be better would be to use a better power supply that includes some filtering on the line side to reduce the disturbance from getting into the converter in the first place. What may also help is putting a suitable line filter in front of the AC input of the power supply.
Even with good components a poor wiring layout could make the situation worse than it could be.