It may have a simply biological basis, as it is proven that pain, just like sensorial pleasures such as sexual contact and sweet foods, causes the release of endorphins, the chemicals that induce pleasure. If a person's pain threshold is sufficiently high, this may outweigh the normal discomfort, even to the point of addiction. Some researchers also believe that algolagniacs physically experience pain differently from other people due to the brain interpreting pain signals "incorrectly" - that is, as pleasurable.
Note that algolagnia is not the same thing as physical masochism, since there are fewer psychological connotations to algolagnia, which may be a simple liking for pain, without any need for domination, submission or humiliation. It may therefore rather take the form of self-infliction of pain. Also, algolagniacs may experience non-sexual pain as pleasurable even if they are not aroused when it begins, whereas most masochists will not enjoy pain from just any cause.