That sort of thing is called an [earworm](https://en.wikipedia.org/wiki/Earworm). According to Wikipedia, the best way to get rid of one is to solve a moderately difficult puzzle, or listen to another familiar song.
It may be explained by Baddeley’s model of working memory, specifically the [phonological loop](https://en.wikipedia.org/wiki/Baddeley%27s_model_of_working_memory#Phonological_loop); a process in your brain that helps you learn and process languages by looping heard or remembered sounds over and over, so you have time to think about them.
Latest Answers