The main function of melanocytes is to produce melanin, which is synthesized within specialized organelles called melanosomes. Melanin plays a crucial role in protecting the skin from ultraviolet (UV) radiation by absorbing and dissipating harmful rays. This pigment is transferred from melanocytes to surrounding keratinocytes, contributing to the overall pigmentation of the skin.