Cramps are about as mysterious to doctors as they are painful. They can strike out of the blue and usually afflict endurance athletes more than others. There is some indication that they at least can be broken down into two primary causes: skeletal-muscle overload and electrolyte deficiency [source: Bergeron].
If you suspect the cause is muscular fatigue, then massaging the affected area should bring relief. If your muscles aren't particular fatigued, but you've been sweating excessively (or not enough), then taking in a half-liter of a carbohydrate/electrolyte drink with a few grams of salt over the course of 5 to 10 minutes should help alleviate the symptoms [source: Bergeron].
To keep cramps from happening in the first place, take in extra electrolytes, stretch before and after exercise and cool down when you're done. It's doubtful any regimen will completely remove your risk of muscle cramps, especially if you're running marathons or triathlons. Like most of the injuries in the article however, you can minimize your risk by not taking on too much training too soon.