Because that’s the law; Article 50 specifically states 2 years.
The main purpose is to give some measure of protection to both sides, in that it gives a time period for terms of the new relationship between the EU and the country leaving to be negotiated; terms that can be very complicated.
The two year period is more important for the country leaving in that the only trade agreement it has is with the EU and through the EU, and all those existing Trade Agreements with the country leaving the EU ceases to exist after the two year period.
So if in those two years the country leaving hasn’t completed negotiating new Trade Agreements with the EU and other countries outside of the EU then at best it’s stuck with WTO (World Trading Organisation) rules e.g. 10% tariffs. This invariably means it’s more expensive to import and export; pushing up prices at home coupled with making that country less competitive aboard, invariably leading to fewer jobs.
The question should be whether the two years is long enough because trade agreements notoriously take years to negotiate; normally a rushed trade deal is usually a bad deal that disadvantages the weaker nation.