Is that the only time you have it? It should be more like tingly, not pain. You might see a proctologist just to see if there is anything amiss. It sounds like a nerve is upset.
Hi Chub, OK First I think your find that there just rectum spasms, if I remeber roight its just something you will have to live with till they go on there own, and second, if masturbating to ejeculation is painful then just masturbate without ejectulation, this should stop the pain.
Good Luck