The culprits of this sad spectacle are blood and gravity. You see, a man's flaccid wang is never actually fully relaxed, provided he's, you know, alive. The muscles on the base are working to stop the blood from flowing into the penis and working its magic. In death, the entire thing goes limp, and as long as the gentleman in our morbid example is supine, seated, or lying on his side, his wiener is going to remain his business. But if he dies in a prone position or a vertical one, Mama Gravity can make blood flow the wrong way and we enter zombie boner territory.
So why is this more common in violent deaths? To explain this in simple terms, we need look no further than auto-erotic asphyxiation, which mimics the effects of trauma-induced angel lust (and has a pretty good chance of causing it if you're not super careful). Hanging and strangling cause damage to the brain and often the spinal cord, which is believed to activate central nervous system-induced arousal -- an effect that can sometimes be seen when the brain is damaged by other means (like poison or bullets). We're still learning about the exact nature of non-gravitational angel lust, so in the meantime, try to lie down as you shuffle off this mortal coil, unless you want to greet the Grim Reaper rockin' a wicked chub. We're not judging you.