Benign moles are extremely common, 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 little or no sunlight ever 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.