The process by which the skin tone changes to a darker shade mainly due to exposure to UV rays in the sunlight is called tanning.
UV rays are harmful and can even cause genetic damage to cells on the outermost layer of skin.
Melanin is a skin derivative that gives skin its colour and protects the skin from harmful UV radiations.
So, the skin tries to prevent damage to the skin by producing the pigment melanin in larger quantities. Excess production of melanin causes the skin tone to change to a darker shade.