We all know about songs going around in our heads. This has particularly become an issue for me because I have to listen to children’s videotapes from Japan (brought over to make sure my half Japanese son gets a good share of Japanese in with his English.)
But the tapes are insidious. I find the tunes running around my brain at 4 o’clock in the morning relentlessly–and then they never stop after a few days of repeat play. All day, every day. And I (thankfully?) understand very little of the actual lyrics, so why do they stay?
The same phenomenon happened recently when I resurrected an old Genesis tape I used to listen to and listened to it several times in the throes of nostalgia. Now I can’t get rid of it. Phil is eating up my synapses.
What is happening inside the brain? Are the same neuronal pathways so over-stimulated that they just keep firing long after the cows have gone home? How can I get rid of the merry-go-round of eternal tunes?
If I hear the Sesame Street jingle in my head again at 5 a.m. I’m going to sing.
Humans like patterns. Our brains thrive on pattern-matching, and so enjoy it that they will not only amplify existing patterns, but will create patterns out of chaos, hence visions of Mary on the burned tortilla, or teddy bears chasing dragons in the clouds. When a pattern comes along that the brain enjoys, it will sieze upon it and go over it as much as possible.
Therein lies the problem.
It isn’t really a conscious process. Your brain is reacting at a very basic level to the patterns you’re feeding it, so the responses are strong and difficult to control. When it happens to me, my brain will overlay' the tune on any repetitive noise I hear, even if the match is imperfect. I will
hear’ the noise modulating to fit the tune my brain has fixated on, even though I know the noise is perfectly steady and regular. I’m sure similar things happen to others. The point is, your brain has glommed onto the pattern and it doesn’t want to let go.
I don’t know any good cure but time and the ability to focus on something else. Screaming and hitting your head is optional. 
. . . and remember:
A pink trip slip for a six-cent fare,
A buff trip slip for an eight-cent fare,
A blue trip slip for a ten-cent fare,
Punch in the presence of the passenjare.
Or something like that.
RR
Punch, brothers, punch,
Punch with care!
Punch in the presensce of the passenjare!

Oliver Saks describes something similar in part of his book “The man who mistook his wife for a hat”. It lacks technical detail about what is really going on (probably because it’s not well understood), but the gist of it is that there were elderly patients who suddenly were “hearing” music. It was always a tune that was familiar to them, but it often was something they hadn’t heard since they were children but had an emotional attachment to at the time. In any case, it was overpowering - like a hallucination, but only auditory. For these patients, his theory was that they had suffered a mild stroke or were having seizures in the temporal lobe (where the primary auditory cortex lives), which was causing the phantom music. This is kind of an extreme version of “getting a song stuck in your head”, so I guess the physiological mechanisms are probably not seizure related for most of us - but it was interesting to read about.
What a great question! As Derleth says, it’s all about patterns with the ol’ monkey brain. Religious chants/mantras utilize this human tendency by replacing normal brain chatter with "higher"patterns of focus toward a more noble goal. I haven’t seen any exact explaination of it, though.
It might also be interesting to know if people who have this problem more severly than others also are prone to obsessiveness in other areas.
I’m guessing that the brain is hard-wired to keep each note in memory permanently or semi-permanently.
In other words, there is a neuron in your brain that holds the slight tremolo of John Lennnon’s voice as he sings the third note of the first line of the refrain to Hard Day’s Night, and it’ll join all the other neurons associated with your memory of the song to start firing whenever you recall it.
I’m dearly hoping this theory is wrong in the case of the song from Barney the Dinosaur. Sometimes strokes that impair long-term memory are a GOOD thing.