U.K.'s Brexit Minister David Davis said that this would allow businesses to plan.
"We are committed to working during this period in good faith and to continuing to respect the principle of loyal cooperation", he said, adding that "this time of transition will be the one during which we will have to finalize our future relationship".
As for citizens' rights, European citizens from the 27 states of the EU who arrive in Britain during the transitional period will have same rights as citizens who arrived before the Brexit, said the deal.
The Brussels negotiator also confirmed he would take the agreement to European Council of EU leaders for approval at Thursday's summit.
The UK has reached a joint legal agreement with the European Union on the legal terms of a Brexit transition deal, a milestone development in the tortured negotiations to extricate Britain from the bloc.
After an "intensive" weekend of talks in Brussels, Barnier said the two sides were "in agreement on a large part" of the divorce treaty governing the terms of Britain's departure after four decades of membership.
"The PM is pleased that we've made more progress in the European Union negotiations and that we are able to give businesses and citizens certainty by the implementation period which will deliver a smooth Brexit and will allow us to trade on the same terms as now until 2020", the spokesman told reporters.
Britain says an EU-UK free trade deal to be sealed by 2021 can do that.
Sterling jumped against the euro on the news as the markets cheered the announcement. "Businesses need not delay investment decisions, or rush through contingency plans based on guesses about the future deal", Davis said.
"We have reached an agreement on the transition period", Barnier told a press conference with Davis.
Mr Barnier said: "What we are presenting to you today, here with David Davis, is a legal text".
This would mean Northern Ireland would stay in EU customs union and parts of the single market if no other solution was found. Ian Wright, Food and Drink Federation (FDF) director general, said it supported a transition period of at least two years.
Negotiators from the European Union and Britain have hailed major progress in the Brexit talks, but conceded there had been no breakthrough on keeping open the Irish border.
May had earlier insisted that no British prime minister could agree to a pact that includes a proposition threatening "the constitutional integrity of the United Kingdom by creating a customs and regulatory border down the Irish Sea".