Benign moles are extremely common on human skin, but their functions (if any) are unknown. There are theories however - the most prominent being that the extra melanin produced in the moles' melanocytes helps to protect against UV radiation. But melanin-rich sites in human skin are often prevalent in areas where sunlight rarely reaches. Further, many nocturnal mammals - bats for example - have skin rich in melanocytes though they are not in any danger from UV in sunlight.
Alternative theories suggest that moles may play a role in immunological defence.
“ […] melanocytes are not simply pigment-producing cells, but produce substances with a range of biological functions, including structural strengthening by cross-linking proteins, antimicrobial defense, photon shielding, and chemoprotection. […] to provide several physiologically significant functions, including the provision of communicatory links with several different systems, e.g. the skin, central nervous system, and immune/ inflammatory responses.”
See: 'The mole theory: primary function of melanocytes and melanin may be antimicrobial defense and immunomodulation (not solar protection)' International Journal of Dermatology, 44, 340–342.
Also see : Melanin and UV protectionplugin-autotooltip__plain plugin-autotooltip_bigMelanin and UV protection
In general, people with darker skin tones (i.e. with more melanin) are less likely to suffer from skin cancers triggered by exposure to high energy photons in sunlight.
"It has been traditionally believed that skin pigmentation is the most important photoprotective factor, since melanin, besides functioning as a broadband UV absorbent, has antioxidant and radical scavenging properties. [