If I knew whether you were a guy or a girl it would help to provide a better answer.
If you're a guy it could be just a spasm of your prostate or rectum you're having.
Spasms and pain inside your rectum can happen due to an enlarged prostate that happens in men over a certain age. I think the age is 40 and above, but younger men can have prostate problems as well.
I'd recommend seeing your doctor about it to make sure it's nothing more serious.