Altered neuromuscular control
Dehydration
Electrolyte depletion
Poor conditioning
Muscle fatigue
Doing a new activity
These are all factors of spasms and cramps and why they occur.
As to how they occur is when a muscle contracts painfully.
Hope this helps!