You are mistakenly falling for the sociobiological ideology that every behaviour is a) genetic and b) has an evolutionary advantage.
There is no evidence whatsoever that genetics control our behaviour, personality, sexuality, etc. Since this behaviour is not controlled by genetics, it is not hereditary and it cannot be selected for at an evolutionary level. If it was as simple as gene = masochism = evolution then we would see masochism running clearly through subsets of the population, the same way that hair colour and appearance run in families. We do not see this, which is further evidence that masochism, whatever its causes, is not selected for at the gene level.
Secondly, as a masochist, pain feels the same to me as it does to anyone else. When masochists stub their toes, bite their tongues or get a cold, they react the same way as non-masochists. What's more, if we didn't feel pain as pain then we probably wouldn't be masochists. I can get pleasure feelings anywhere but I do pain play because it is painful. If being whipped felt the same as being kissed then I wouldn't bother with being whipped! It would be illogical to risk injury for a pleasure feeling that you could get in a non-risky way.
As regards sadism and evolution, the same scientific facts apply; there is no evidence that sadism is controlled by genetics or selected for on the evolutionary scale. However, even if it was possible to genetically cause and select for sadism, it is unlikely that sadists (in the psychopathic / Sadean sense) would be selected for as physical aggression is highly risky and only undertaken by most animals when they feel that they have no other options. This is why animals have developed a long list of display behaviour to settle disputes without risking physical aggression - such as barking, puffing up to look bigger, digging at the ground, marking territory, etc. Murdering other individuals where there is no reason is very, very rare in the animal kingdom even amongst primates. It is likely that such behaviour is more a product of human socialisation, than any natural or beneficial reason.
Finally, even if such an individual was successful at murdering other individuals, evolution would eventually select against them as humans are social animals and require the comfort and security of groups to live successfully. An individual that went around randomly murdering or torturing its fellow group members would very quickly find itself without a group, for one reason or another.
As regards the real reason for masochism - humans are incredibly sexually creative. We seem to delight in finding new ways to enjoy our sexual instinct, way beyond what any other animal will do. It is not sexual orientation that is imprinted in our genetics, it is a lack of sexual orientation and the freedom that gives us to explore is amazing. It is most likely a combination of pre-conscious experiences not long after birth and a certain amount of free will that dictates our sexualities. I know that I always found masochism appealing, but it has been my decision to explore and expand on that interest when I could have gone in different ways. I'm sure the same is true for most masochists and sadists.