No matter how evil the villains are, the good guys can't just kill them: heroes are supposed to be better than that. They need to stay pure and noble (or innocent); role models, exemplars of solving their problems without resorting to bloodshed. If they take another person's life, no matter how justified, they will lose their moral edge, but when the villains are just arrested and hauled away by the police, this isn't satisfying. For one, they have a tendency to escape. For another, the mundane workings of the criminal justice system seem woefully inadequate to hold or to punish a really evil villain. We, the viewers, want to see real justice administered, and we don't trust human hands (or at least not ''heroic'' human hands) to administer it.
So, the writers arrange for the villain to die in a manner that is completely their own fault, or, at least, obviously not the hero's. If they die right in the act of attempting to kill the hero, this gives a particularly nice karmic zing. If they attack after being defeated and then spared by the hero, this is one of the rare circumstances where the hero can dispatch the villain personally and still come across as blameless.
Note that this only applies if the villain is clearly human or the setting's equivalent. If they change into some kind of monster, they are no longer protected by this trope: the hero might hesitate to kill another human, but a mutated, horrendous beast is fair game — doubly so when the villain took this form for the sole purpose of murdering the hero. The trope may still apply if the villain's inhuman nature somehow allows him to escape justice at the mortal heroes' hands; in such a case, their doom would come from a completely unexpected quarter, such as previously abused minions finding and shattering the villain's Soul Jar to avenge themselves, without any involvement from the heroes whatsoever (and the minions possibly not even pulling a Heel–Face Turn) and if a Karma Houdini finally becomes the receiving end of this trope, this is Karma Houdini Warranty.
It's more common in Western markets, as a result of heavy censorship and the general reluctance among writers to feature their character (usually in a show with a younger demographic) doing such acts as killing, especially if they're underage. Occasionally known by the older demographic as "getting one's comeuppance." Given that there is a certain charm to Self Disposing Villainy, this trope can show up in works that allow the hero to kill people; it's just that it's much more common for it to show up in situations where the hero has a no-kill policy for one reason or another.
This trope is less common in more cynical works, where the good guys using lethal force is not only more expected but the refusal for a hero to kill comes off as naive at best and irresponsible at worst.
Karmic Death is an example of Death by Irony. Disney Villain Death, The Dog Bites Back and Just Desserts are subtropes. This trope is the opposite of Karmic Jackpot.
Compare Asshole Victim, Hoist by His Own Petard, A Taste of Their Own Medicine and Karmic Butt-Monkey. See also Cruel Mercy. Adaptational Self-Defense usually involves this. The Killer Becomes the Killed is a Crime and Punishment Series variant.