No one really knows why...there are various theories as to why the diaphragm spasms. It could be the result of tugging on the diaphragm by the ligaments that attach the internal organs, such as the liver, to the muscle. Another explanation may be that exercising intensely decreases blood flow to the diaphragm, causing it to go into spasm.
Also, raising the knees to run contracts the belly muscles, which increases pressure inside the belly and presses on the diaphragm from below.
Additionally, during exercise, air tends to get into the lungs more easily than it gets out, so the lungs fill with air and press on the diaphragm from above. The dual pressure may squeeze the diaphragm and briefly shut off its blood flow, resulting in cramping.
Other theories are that the pain results from gas trapped in the large intestine or that it is brought on by exercising too soon after eating.
So, if the cause of side stitch is anything to do with the last 2 theories it's simple - don't eat or drink too close to exercising (especially food that is hard to digest) or release some of that trapped gas!