I was told that muscle cramps are caused by a build-up of lactic acid in the muscles. Cramps most commonly occur after exercise if you push yourself too far too fast.
Muscles obtain their supply of oxygen from the blood and they use aerobic respiration to create energy. If a person is not used to a lot of exercise, then the heart and lungs might not be able to supply the necessary amount of oxygen, so the muscles use anaerobic respiration to produce lactic acid. This, however, is only as a last resort. The cramps occur about a day or so later.
I have found that the best way to rid oneself of muscle cramps is the gently strech the afflicted area until cramping subsides.
I am basing the explanation on isolated nuggets of information strung together with logic. Hopefully I am correct, but correction would be welcome. There is a previous article about muscle stiffness in the New Scientist book 'Does Anything Eat Wasps?' , called 'Delayed Reaction' if that would be of any interest to you.
Hope I was helpful :)