The anal sphincter, the group of muscles that open and close the anus, are no different than other muscles of the body in that they can spasm when irritated.
In addition to a avoiding spices, one should also maintain a diet
high in fiber
to keep stools soft and regular. Be sure to see you doctor if the symptoms continue, as there can be other causes as well.