@naillit
Old_Geezer already gave the answer I wanted to give, that is that non-procreative activity was, in ancient times, perceived as a threat to a tribe which is locked in perpetual conflict with neighbouring tribes.
I would expand on this by adding that, the Darwinian term 'fitness' was never about physical fitness (the modern understanding of it); the fastest runners, for example, might also have the highest levels of hunger or thirst and thus be the first to keel over in a prolonged drought. Instead, Darwin referred to "degree of fit" to the environment, the objective measure of which was reproductive success.
With two competing species, in a restricted environment (his Galapagos Islands visit inspired this line of thinking) there is no need for physical combat, one only needs to reproduce faster than the other in order to dominate local resources and squeeze the competitor out (ie without really trying). *
Humans simply shortcut this process and Neanderthals are no more (give or take some interbreeding, by all accounts). Even chimpanzees have the organisational ability to send foraging parties onto some other group's 'patch' and have been observed to dish out violence if they encounter a rival unsupported by its fellows.
So, why kill sexual transgressors? Why not imprison them? Back in primitive times, food was scarce and the death penalty was the logical solution to not wasting food on citizens rendered unproductive by incarceration. Prisons were to contain people held for ransom (eg POWs) or who had got into debt.
* Footnote:
In practice, Galapagos finches diversified their beak shapes so that they each exploited different food sources best and -avoided competition- with each other,
hence dozens of species survived, where you would expect there to be barely room to support one.
I mean, you'd expect a creator to be content with just one (of each "kind", to use a creationist buzzword).