Melanin is a pigment produced by melanocytes in the skin, hair, and eyes. Its primary function is to absorb harmful ultraviolet (UV) radiation from sunlight, thereby protecting the cells from DNA damage and reducing the risk of skin cancer.