It has been there for centuries. Classic case. Aboriginals vs the fresh immigrants.
Fresh immigrants were better equipped and forced themselves on to subcontinent. Native people resisted and later on exiled as they did not accept the rules of the immigrants.
Natives were discriminated and not killed unlike white people who wiped out the native people and called it their ow country.
Now there are tons of people who have mixed with the native people. It even gets hard to tell them apart in some cases. Initially there was a lot of mixing. Many could even cross the caste barrier and become anybody.
Hence it is not a surprise if you can find a light skinned Dalit and a dark skinned upper caste fellow.
But Sage Manu wrote some rules which prevented further mixing. He drew strict lines between castes and emphasized that nobody should cross that boundary. Most accepted the rules of Manu except the native people (Dalits). Hence Manu did not include them in caste system and Dalits were treated as outcastes and it is a crime for anyone to even talk to Dalits as Dalits were considered impure. Till today it has become a tradition even though it is slowly changing.
But what remained was the concept of untouchability. They are still treated with the same attitude by some upper caste people in some remote places.