Levirate marriage is when the brother of a deceased man is obliged to marry his brother's widow. It was practiced in the Ancient Near East, Asia, India, etc.
Genesis 38:
Genesis 38:
That was Onan's levirate duty.6 Judah took a wife for Er his firstborn, and her name was Tamar. 7But Er, Judah’s firstborn, was wicked in the sight of the LORD, and the LORD put him to death. 8Then Judah said to Onan, “Go in to your brother’s wife and perform the duty of a brother-in-law to her, and raise up offspring for your brother.”
That wasn't masturbation but coitus interruptus.9 But Onan knew that the offspring would not be his. So whenever he went in to his brother’s wife he would waste the semen on the ground, so as not to give offspring to his brother.
The Lord put Onan to death because he refused to impregnate his dead brother's wife.10 And what he did was wicked in the sight of the LORD, and he put him to death also.