My question is what causes menstruation and why is it painful?

Hormones... Normal menstruation is initiated by a drop in your body's progesterone hormone level. The lining of the uterus loses is hormonal support and is shed from the uterus. Its painful because the shedding process releases inflammation hormones such as prostaglandins. The uterus also contracts to get blood and clots out of the uterus and this causes labor-like pain. Best wishes.