It is immoral and constitutes grave matter.
So if it is committed with full knowledge of its immorality and with complete consent, then it is a mortal sin.
However, if the masturbating person EITHER does not know that it is gravely immoral OR does not completely consent to the act OR both, it is not a mortal sin, but may be a venial sin.
Hope this helps.
—Soler.