Muscle contraction occurs when the brain tells the body to move. The brain then starts an action potential down the motor neurons, until it reaches the terminal bouton. At the terminal bouton, it releases the neurotransmitter, acetylcholine, which travels through the myoneural junction and into the myoneural cleft. ACH binds to the receptor, which causes an action potential in both directions along the cell membrane.
The action potential repels the potassium, which travels down the cell membrane until it falls into a transverse tubule. K+ continues to fall into a transverse tubule and accumulates, which increases voltage (-70 to -50 mV). The voltage change causes the calcium gates to open and to diffuse the calcium.
Calcium then binds to troponin, which in turn binds to tropomyosin and pulls it, exposing the g-actin binding site that allows myosin and actin to bind. The myosin head changes shape (called power or working stroke) and pulls the actin towards the M line, and the muscle contracts. Similar to a tug of war, the myosin heads (your hands) pull on the actin (the rope) to contract a muscle. Like team members in a tug of war, the myosin heads alternate between pulling and holding on to the actin; the only way that myosin will release actin is to add ATP, which forces the two apart and thus relaxes the muscle (as mentioned above).
Now, a lack of potassium, sodium, or calcium would prevent the muscle from contracting but won't relieve a muscle cramp. The common advice of eating a banana actually does help relieve cramps but not because of the potassium. Bananas also have sugar and fat, which are converted into ATP.
Muscle cramps are primarily caused by a lack of ATP in the body. ATP forces the myosin to release the actin; thus, the muscle relaxes and the cramp is relieved.
Another cause could be the lack of magnesium, which helps the ATPase sodium/potassium pump, which, in turn, returns the voltage to resting potential and relaxes the muscle.