Kurt
Old Testament, from the book of Genesis:
Then Judah said to Onan, “Go in to your brother’s wife and perform the duty of a brother-in-law to her; raise up offspring for your brother.”
9 But since Onan knew that the offspring would not be his, he spilled his semen on the ground whenever he went in to his brother’s wife, so that he would not give offspring to his brother.
10 What he did was displeasing in the sight of the Lord, and he put him to death also.
In other words:
Back in those times if your brother was not able to father children, you were expected to 'help out' and get his wife pregnant.
Not doing so but masturbation before meeting his wife for the socially required time together to get her pregnant, so that there was no impregnating her, was a sin.
Over the years that has been interpreted as any form of masturbation being a sin.