Its an anti-afterfire solenoid - what it does is shut off fuel flow through the main nozzle while engine is shutting down - prevents fuel from being sucked in and not being burned due to no spark, which results in backfires (and flames out muffler or carb) which can start fires and cause other damage.
Reversing the wires, 2 possibilities - one is the solenoid has a diode in it that delays the solenoid from closing when , for example, starting the electric clutch or battery voltage drops too low - that diode is a one way valve , or two, wires were not making a good connection and switching them around resulted in having a complete connection.
Side Note:
If the solenoid fails , sticks closed, or no current to operate solenoid due to wiring issues, the solenoid prevents fuel from flowing in the main nozzle, which also feeds High idle circuit so it acts like it isn't getting gas.
Many engines like this are set up a little more sensitive on the governor so even if shutting down after idle, the throttle plate may still open up , pulling fuel from the main nozzle while engine winds down and governor opens throttle.. so even removing or bypassing the solenoid and "shut down at idle" advice that some people give, is not always effective or safe - only if you can ensure the throttle plate stays closed at idle even when engine is not running , would it be safe enough to bypass that solenoid - Of course if you don't mind the backfire (damaging expensive exhaust systems or blowing a hole in air filter) and risking a fire, then by all means it can be bypassed, but don't say you were not warned.

How poor are they who have not patience. What wound did ever heal, but by degrees? - Iago (Othello Act II, Scene 3)