This way of connecting a diode to an inductive load is called flyback or freewheeling diode.
There are many resources online to read up on how and why they are used and hence little point reiterating this here IMO.
e.g.
In short they “short-circuit” the induced, opposing voltage caused by the collapsing magnetic field of the coil when the driving voltage and hence current is switched off.