Pain is a mechanism for Self-Protection. It can be considered as a very strong form of cognitive dissonance.