What causes spasm of rectal sphincter?

Anal fissure. Usually it is due to an anal fissure, aka; cut. Stool softener and warm bath usually improves in 1-2 weeks. And it is the anal sphincter.