I do this sometimes. And sometimes a word or phrase will pop into my head with no idea where it came from and I will have to repeat it until I am able to let it go. But I am a very big counter. This is one of my strongest OCD symptoms that has been a constant battle for many years. I count the letters in words and the words in phrases more than I focus on the actual words or phrases themselves. It can be very distracting. However, I think that since I have been "counting" for sooooo many years that now I am able (most of the time, unless I am stressed or angry) to listen or carry on a conversation with a stream of numbers in the background...Almost like having two radio stations on at once, but being able to focus on both at the same time. So, in an odd way, I may be kind of lucky...Except I have a ton of trouble in math classes!
I don't know of any exercises that would help you with yours though. If it happens more when you are stressed, or when things are chaotic, you could try to calm down (maybe through meditation or exercises like pilates or yoga or anything that you find soothing) before you go to class or have to do something that takes a lot of strict attention. I am sorry that I could not be of more help.