Perhaps the first marriage was invalid and a way can be found for reconciliation and a return to the sacraments. Contact a priest. It seems people decide that for themselves. I don t see why it would be wrong. According to the Catechism of the Catholic Church , an act of masturbation in itself is serious matter for sin.
Any mortal sin requires two other elements.
I keep saying it is not always easy to judge how much freedom and consent or awareness are present in an act. Sometimes a confessor will have much difficulty evaluating those things. Sometimes the individual himself or herself will be asking whether he or she fully intended such an act, acted with full freedom. Theologians and psychologists may differ about how compulsive an action may have been. In such cases a person should confess the act and circumstances and put them in God s hands and to God s mercy.