Muscles contract only because they are told to by the nervous system. The brain creates the signal and passes it down through the spinal cord into the carrier nerves (the large nerves which exit the spinal cord). These then divide and divide again as they travel towards their destination. They terminate as local nerves which make direct contact with the muscle.
When the signal arrives, it triggers the muscle to release some positively charged ions (e.g. calcium being the main one) which in turn starts the process of muscle contraction.
A muscle contraction is simply a muscle shortening and thus pulling on the bones it is attached to. The process involves thousands of fibres aligned in parallel sliding past one another towards a central point thus decreasing the overall length of the muscle. Throughout the process, like all cells, the muscle will use up energy which is made from oxygen and sugars. It will also create waste products that are removed via the blood.
A muscle spasm differs from a regular contraction because it very strong, prolonged and unplanned. The nervous system is ultimately responsible because it controls the muscle. The fault can occur anywhere in the nervous system as the following paragraphs explain.
1) Brain and spinal cord
The brain / spinal cord lose effective control of a portion of the muscular system leading to ill timed, uncoordinated and strong contractions. This occurs following spinal cord or brain injuries as well as disease processes such as dystonia.
2) Carrier nerve
Commonly referred to as a trapped nerve. The mechanical compression associated with the entrapment irritates the nerve making it hyper-sensitive and hyper excitable. Thus any signal travelling down this route is likely to be distorted and amplified. An amplified signal will create a large response in the muscle, hence a muscle spasm.
3) Local nerve
The local nerves make direct contact with the muscle and thereby share the same environment. We know from above that nerves are sensitive to changes in their local surrounding. Therefore, excessive waste products from the muscles will irritate the local nerves.
Muscles generally produce little waste however if they are over-used or do not have enough oxygen or energy they will produce more waste. If this situation continues for a long time the waste products build up and start irritating the local nerve making it hyper-excitable. The consequence of excitability is more signals asking the very same muscle that is producing excess waste to contract more. More contraction equals more waste which equals greater excitability. This cycle will culminate in a muscle spasm.
4) Ions / water concentration changes in the body
In these cases the muscle spasms tend to be body wide (i.e. not localised to a single muscle group). They again occur because the nervous system as a whole becomes hyper-excitable. Remember, our body work within a set range of tolerances with respect to a number of variables such as temperature, water/ion concentrations, pH, etc. Deviations outside these tolerances creates side effects, one of those being a hyper-excitable nervous system.
The most common reasons for muscle cramps are related to changes in ion/water concentrations as well as irritations to the local nerves (i.e. points 3 and 4).