Google causes of cramps. Be ready to read for hours and hours. There is no single definitive answer. Actually, you will find many people with single definitive answers. There was a guy with a theory about the muscle being confused... thinks it's stretching too far and is contracting to protect itself. I don't find it now, but it was the most plausible explanation I've ever read.
IMO, speaking technically

, you rode beyond your ability, your muscles' chemical/electrical/mechanical systems got screwed up, and they went bezerk. You need to either not ride in hills with guys faster than you, or do it more often.