What many people do not know is that the erection is a passive action. In fact, when men do not have erection, the penis makes an effort: the smooth muscles of the arteries at the base of the penis must stay contracted, in order to impede the blood filling the penis. (In fact, most aphrodisiacs, including Viagra, contain a chemical(s) that relax these muscles).
No wonder dead people can have erections. A death erection (also named "angel lust") is a postmortem erection happening when the man dies vertically or face-down and the corpse remains so.
When the body is dead, the smooth muscles of the arteries relax (in fact, die) but this is not enough: the blood cannot enter the penis as the heart no longer pumps it, creating pressure for entering the penis' vessels. In the dead body, only gravitation acts on the blood. This means that the blood accumulates at the lowest point of the body, provoking a swelling of the dead tissue; the upper located part of the body discolors, leaving the dead body's lividity. In the mentioned cases, the blood enters by gravitation into the dead penis, causing an erection.
When a person is hanged, the blood will accumulate in its lower part: legs and feet. If the legs are filled with blood, the blood in the waist can causes an erection, entering the penis's erectile tissue. As long as the blood is not clotted, the body must stay in that position to maintain the death erection.
Death erection can also be provoked by a traumatic death, especially asphyxiation (like in strangulation or hanging), being induced by an arousal which, of course, have very little to do with sex, accompanying the final spasms of the nervous system, and in this case ejaculation may take place. Some link this to brain or spinal cord injuries caused by the noose, as priapism in living men has been associated with such conditions.
On war time, death erection made an excellent war trophy, a method of humiliating the enemy by displaying their posthumous copulating organ.
Hanged women too can present a corespondent to this, translated trough engorged labia and vaginal discharges.
Other rapid or violent deaths too can cause angel lust: bullet wounds to the brain or large blood vessels or poisoning.