The physics of why that can happen, when you energize the starter current flow starts from zero and increases over time in the various starter coils. Current can go to 100 amps or more in the motor coils, 20 amps in the solenoid coil. Either way, that’s a lot of current, and big magnetic field develops. If that circuit is then suddenly disconnected from its power source – either b/c you turn the key from start to on, or the armature motor brushes switch from one segment to the next — current flow in the coil suddenly goes to zero. The voltage from one end of a coil to the other is proportional to how fast the current changes in amps per second, multiplied by the inductance of the coil, so all in all a big voltage can develop in that situation. Hence, a spark. It takes close to 1000 volts I think to produce a sizeable spark in air.
I should say that I’ve never seen a spark from a starter motor myself like that. The connections that get disconnected suddenly and which could produce a spark are inside the motor case on every starter motor I’ve come across, so any spark that occurred wouldn’t be visible. That’s why the posters above are suggesting a loose connection is involved somewhere, as a spark could occur outside the case then, at the loose connection.