Muscle cramps are caused by the accumulation of lactic acid during anaerobic respiration in the muscles. When oxygen supply is insufficient, muscles switch to anaerobic respiration, leading to the production of lactic acid, which causes cramps.