The most common cause for anal pain, especially when associated with a bowel movement and is ongoing for an extended period of time is an anal fissure. Constipation is often the root cause of this tear that occurs at the anus, which then leads to spasm of the surrounding muscle - sphincter.
Escalating severe pain not always directly related to bowel movements in timing could be a sign of fistula or abscess and requires more urgent attention.
Uncomplicated fissures can often heal with good medical management but recurrence is common especially from inadequate duration of therapy.