The answer to this question can be found in the Book of Exodus, specifically in chapters 12-19. In these chapters, it is explained that when the Israelites journeyed out of Egypt and were wandering in the desert, they were commanded to sacrifice animals to God. The firstborn sons of the Israelites were considered holy, and so they were not allowed to participate in this sacrificial ritual. Instead, the firstborn sons of the Levites were chosen to take part in this ceremony.
When the Israelites arrived at Mount Sinai, they were still under the commandment to sacrifice animals to God. However, when Moses ascended Mount Sinai with the tablets containing the Torah, he was also given specific instructions about how this sacrifice should be performed. One of these instructions was that the firstborn sons of Levi should not participate in this ceremony. Therefore, because many of the firstborn sons of Levi had already died during their wanderings in the desert, this tribe lost its set apart