Primary dysmenorrhea is caused by uterine contractions triggered by prostaglandins, hormone-like substances involved in pain and inflammation. Higher levels of prostaglandins are associated with more severe menstrual cramps. Secondary dysmenorrhea can be caused by conditions like pelvic inflammatory disease (PID), adenomyosis, or ovarian cysts.