I've heard some hypothesizing that they may be a consequence of free will. Specifically, your lizard brain repeatedly realizes that thanks to mammal brain, it can do whatever it wants and tells you to do something you won't. I don't really buy into that, but the idea is out there.
Personally? I think it's a kind of emergency action system that keeps extreme events in reserve so you can act if you need to. I think this because in my intrusive thoughts, which are near 100% violent, it all comes down to the sudden ambuHE DOESN'T SUSPECT A THING, SMASH HIS FOOT AND JAM THE SCISSORS THROUGH HIS EYES.
Basically, I think it keeps me prepared to kill everyone around me at all times just in case I need to.
I do have some non-violent intrusive thoughts, but they're weird. They technically qualify as blasphemous, but being non-religious, they end up being what I've called anti-blasphemous, in which I have a sudden existential fear over the existence of a deity and a desire to become religious. Of course, this has no more sway over me than scissors-through-the-eyes voice does.
I'd say that the evolutionary basis for the blasphemous thoughts are similar, but instead of killing everyone it is about being prepared to break with societal standards or your own viewpoint in the event of a crisis. It is thus more accurately described as taboo thoughts than blasphemous ones, religion just comes up a lot.