I know it's bad form to paste text things, and still this is not directly related to masturbation and religion, my views could be pretty much summed up by someone else:
Overview
Freud enumerates what he sees as the fundamental tensions between civilization and the individual. The primary friction, he asserts, stems from the individual's quest for instinctive freedom and
civilization's contrary demand for
conformity and repression of instincts. Freud states that when any situation that is desired by the pleasure principle is prolonged, it creates a feeling of mild contentment. Many of humankind's primitive instincts (for example, the desire to kill and the insatiable craving for sexual gratification) are clearly harmful to the well-being of a human community. As a result, civilization creates laws that prohibit killing, rape, and adultery, and it implements severe punishments if these rules are broken. Thus our possibilities for happiness are restricted by the law. This process, argues Freud, is an inherent quality of civilization that gives rise to perpetual feelings of discontent among its citizens.
Freud's theory is based on the notion that humans have certain characteristic
instincts that are immutable[
citation needed]. These include, most notably, the
desires for sex, and the predisposition to violent aggression towards authority figures and sexual competitors, who obstruct the individual's path to gratification.
https://en.wikipedia.org/wiki/Civilization_and_Its_Discontents
Thought maybe it could be of some interest.