Magnesium does NOT cause headaches.
Magnesium is actually better known for reducing headache pain rather than causing it.
Research indicates that up to 50% of patients suffering from acute migraine attacks have are found to have low magnesium levels. Magnesium is responsible for more than 300 biochemical reactions in the body including those involving migraine related neurotransmitters and receptors such as nitric oxide synthesis and release as well as effecting serotonin receptors. Though it isn't quite clear the exact role that magnesium levels play in the onset of headache symptoms, several studies have established that the administration of magnesium can treat the symptoms effectively. 
Magnesium Supplementation and Headaches
Research is indicating that oral magnesium supplementation may be effective in preventing headaches. One study concluded that supplementation of 600 mg of magnesium ((trimagnesium dicitrate) daily, reduced migraine symptoms in 41.6% of the participating subjects by week 9. Though the study was successful, 18% of the subjects did experience diarrhea, while 4% experienced gastric irritation, which are common side-effects of excessive magnesium intake. 
Intravenous Magnesium and Headaches
When the magnesium is administered intravenously rather than orally, the mineral's impact on headaches can be even more astonishing. One study found that four out of five patients experienced a complete elimination of their headache pain within 15 minutes of an intravenous injection. 
Except for brief flushing, no other side effects were observed. Intriguingly, those patients who failed to respond to the intravenous injections had, in a majority of cases, a higher level of magnesium in their blood than those responding. In other words, for whatever reason, their metabolism failed to conventionally ingest the intravenously administered jolts.
More Research Required
The lion's share of scientific and anecdotal data confirms this idea of a positive, rather than negative, link between magnesium and headaches. There are also a number of doctor-authored books about this subject.
In one of these, a practitioner explains how his own health battles led him to discover the widespread link between magnesium deficiency and various ailments, including headaches. He argues that women are more susceptible to these types of deficiencies than men, that the headaches experienced by each sex are very different, and that magnesium's impacts are far less understood in the GP medical community than those, say, of calcium. 
Compounding the lack of knowledge in this mineral area is the fact that the standard test for magnesium deficiency can often be faulty. Once a person gains an understanding of the different kinds of medical magnesium products and where the compound is stored in the body, they can begin to get closer to proving this beneficial link between the mineral and reduced headache pain.