Numbers 3:45

Take the Levites instead of all the firstborn among the children of Israel, and the cattle of the Levites instead of their cattle; and the Levites shall be mine: I am the LORD.

The Lord set apart the tribe of Levi to carry out the sanctuary service in the place of all the firstborn sons of Israel. The firstborn themselves still belonged to the Lord, so each one was to be bought back by the payment of a ransom price. This exchange was made because the Lord had spared Israel's firstborn when His judgment fell upon Egypt.