Question :

Is it permissible for one to masturbate in order not to commit fornication?

The Answer :

This is called committing the milder of the two evils such as eating a carcass in fear of death, and this happens when committing fornication is very likely to occur.