The Buddha taught that on the path to enlightenment, sexual desire can, and eventually must, be completely transcended.
To this end, monks undertake an absolute form of celibacy in order to isolate and reveal the impermanent, unsatisfactory and impersonal nature of sexual desire, and thus uproot identification with it. The weight of the Discipline is thrown behind this practice by making intentional emission of semen one of its most serious offences (saṅghādisesa).
If committed, it necessitates a period of penance and rehabilitation that is deeply embarrassing to the transgressor (he has to, for instance, publicly confess his offence to the Sangha on every day of the penance) and inconvenient for the community of monks. Even if he stops short of masturbation, a monk who makes the slightest deliberate attempt to excite himself sexually or physically relieve sexual feelings, commits an offence nonetheless (albeit of a less grave nature).
He is given, therefore, absolutely no choice but to face up to the tension of lust. Until insight arises, a monk must be sustained by patient endurance, wise reflection, calmness of mind and confidence in the value of struggling with such feelings.
This reflection by Ajahn Jayasaro is from the book, Stillness Flowing, (pdf) p.82.