In order to be moral, each and every sexual act must be marital and unitive and procreative. The good moral object of every moral sexual act is these three meanings intended by God: marital, unitive, procreative.
The type of sexual act called masturbation is not marital, is not unitive, and is not procreative. Therefore, masturbation has an evil moral object and is an intrinsically evil act. Intrinsically evil acts are always immoral. But intrinsically evil sexual acts are always gravely immoral.
Sacred Congregation for the Doctrine of the Faith: “Now according to Christian tradition and the Church’s teaching, and as right reason also recognizes, the moral order of sexuality involves such high values of human life that every direct violation of this order is objectively serious.” (Persona Humana, X)
The sin of masturbation is intrinsically evil, due to the deprivation of the three meanings required by God for every deliberate expression of the sexual faculty.
There are three fonts (or sources) of morality: 1. intention, 2. moral object, 3. circumstances.
When any act is intrinsically evil, due to an evil moral object, neither a good intention (the purpose for which the act was chosen), nor dire circumstances, can make the act moral. Intrinsically evil acts are always immoral.
What makes an act moral? Three good fonts. No bad fonts.
Catechism of the Catholic Church: “2352 By masturbation is to be understood the deliberate stimulation of the genital organs in order to derive sexual pleasure. “Both the Magisterium of the Church, in the course of a constant tradition, and the moral sense of the faithful have been in no doubt and have firmly maintained that masturbation is an intrinsically and gravely disordered action.” “The deliberate use of the sexual faculty, for whatever reason, outside of marriage is essentially contrary to its purpose.” [Persona Humana 9]
The CCC includes, in the definition of masturbation, a statement of the most common purpose, to derive sexual pleasure. However, the same text also teaches that masturbation is intrinsically and gravely disordered, which is another way of saying that the act is intrinsically evil and a grave sin (a mortal sin). Intrinsically evil acts are immoral regardless of intention or circumstances (CCC, n. 1756). Therefore, even though the Catechism includes a statement of the usual intention (first font) that accompanies the sin of masturbation, the act remains intrinsically evil and gravely immoral, even when the purpose is not to derive sexual pleasure.
Is masturbation justified when the purpose for the act is to obtain a specimen for medical analysis, for example, when a married couple have a problem with fertility? No, it is not. The purpose for which the act is chosen is the first font of intention (the intended end). Intrinsically evil acts are immoral due to an evil moral object in the second font. A good purpose, such as to diagnose a medical problem, does not remedy the deprivation of the marital, unitive, and procreative meanings in the moral object. Therefore, the act remains intrinsically evil and always gravely immoral, regardless of intention or circumstances.
Confirmation that this is the correct analysis is found in the Instruction Donum Vitae, which condemns masturbation used for the purpose of procreation by in vitro fertilization (IVF). The document makes a point of condemning IVF, even when considered apart from the grave sins of abortion and masturbation that usually accompany IVF (DV n. 5). In other words, masturbation is condemned as an act that is inherently immoral, regardless of its purpose.
Is masturbation justified within marriage, when one spouse masturbates the other spouse before, during, or after natural intercourse? No, it is not. The act of masturbation remains intrinsically evil due to the absence of the unitive and procreative meanings from the moral object of that act. (Neither is such an act truly marital, as God intends the union of the spouses.) The good moral object of a one act, no matter when it occurs, cannot remedy the evil moral object of a distinct act because, in order to be moral, each and every act must have three good fonts. The fonts that apply to any act are only those that spring up from that same act. In other words, the combination of a good act and an intrinsically evil act cannot make the intrinsically evil act good. It remains an inherently disordered act.