Turkey extracted a string of political and financial concessions in exchange for becoming a bulwark against the flow of desperate humanity heading to Europe from Syria and elsewhere.
"It is a historic day because we reached a very important agreement between Turkey and the EU," Turkish Prime Minister Ahmet Davutoglu said after the deal was struck at a summit in Brussels.
"We today realised that Turkey and the EU have the same destiny, the same challenges and the same future."
EU president Donald Tusk said that under the deal, all "irregular" migrants would be returned to Turkey from Sunday.
For every Syrian refugee expelled, the EU would resettle one directly from Turkey.
Tusk said the deal would only work as part of a broader plan, including support for Greece, the main point of entry for migrants to Europe, and cutting the flow of refugees through the Balkans to Germany.
"Some may think this agreement is a silver bullet but reality is more complex," said Tusk, who has played a leading role in a crisis that has seen 1.2 million asylum seekers reach Europe since January 2015.
- 'Herculean task' -
Around 4,000 people including women and children have drowned crossing the Aegean Sea in flimsy smugglers' boats, including 400 this year alone.
For its cooperation to stem the flow, Turkey won an acceleration of its long-stalled bid for EU membership, the doubling of refugee aid to six billion euros ($6.8 billion) and visa-free travel for its nationals to Europe's Schengen passport-free zone by June.
But there remained huge doubts about how to implement such a scheme, not least due to still often-tense relations between Ankara and Brussels.
"This is a Herculean task facing us," European Commission chief Jean-Claude Juncker told the press conference.
He said some 4,000 border officials and other experts will need to start working immediately on implementing the deal that will cost the EU up to 300 million euros over six months.
EU officials stressed that each application will be treated individually, with full rights of appeal and proper oversight.
Turkish officials as well as UNHCR officials will be sent to the Greek islands to oversee the scheme.
The deal also envisages major aid for Greece, where tens of thousands of refugees are already trapped in dire conditions after Balkan countries shut their borders.
The deal will not affect the 46,000 migrants already in Greece, who will either be expelled as economic migrants or granted asylum.
- 'Don't trade refugees -
The United Nations and rights groups fear the deal could violate international law that forbids the mass deportation of refugees.
The UN High Commissioner for Refugees (UNHCR) stressed the right to asylum must be paramount.
"Ultimately, the response must be about addressing the compelling needs of individuals fleeing war and persecution. Refugees need protection, not rejection," it said in a statement.
Amnesty International set up a sign outside the summit venue saying: "Don't trade refugees".
The deal a "historic blow to human rights," Amnesty said.
John Dalhuisen, Amnesty director for Europe and Central Asia accused the EU of seeking to "wilfully ignore its international obligations”.
EU states have expressed concerns about Ankara's human rights record, including its treatment of the Kurds and a crackdown on critics of the government.
Far from the smiles in Brussels, Turkish President Recep Tayyip Erdogan blasted the EU for taking a "handful of refugees" in contrast to the nearly three million Turkey is hosting.
Erdogan also accused the Europeans of supporting the outlawed Kurdistan Workers' Party (PKK) days after a bombing in Ankara claimed by Kurdish rebels allegedly linked to the group.
"European countries are paying no attention, as if they are dancing in a minefield," he said.
But one major hurdle that was overcome was opposition from Cyprus, which has long-standing tensions with Turkey over Ankara's refusal to recognise its government on the divided island.
The migrant crisis has left Europe increasingly divided, with fears that its Schengen passport-free zone could collapse as states reintroduce border controls and concerns over the rise of populism and anti-immigrant sentiment.
Greek Interior Minister Panagiotis Kouroumblis described the overwhelmed border town of Idomeni where many of the migrants are camped out as a "modern-day Dachau".
Some migrants, speaking in a rain-sodden makeshift camp, told AFP said they would stay, others that they would try to get across the border, and others said they were against moving to reception centres.
Imen, a 17-year-old girl travelling with her brother, mother and two aunts, said, "We have to meet our father who is in Germany now. He left a few months ago to wait for us there. What are we going to do?"