Spiritual masochism suggests an act of self-inflicted pain, such as self-flagellation.

The pain perversely brings pleasure.

It can be an aspect of martyrdom.