Sometimes it can happen that you get a headache during masturbation / masturbation. Pain that occurs after orgasm is called dyspareunia. Many different reasons can be causative for this pain.
Organic changes (e.g. inflammations, adhesions) but also psychological causes (e.g. stress, relationship conflicts) can be the trigger of this pain. If pain occurs during self-fire, you should first wait, if it lasts for several weeks, you may need to see a doctor.