BRUSSELS (Reuters) - European Union leaders agreed unanimously on a package of measures aimed at keeping Britain in the 28-nation bloc at an extended summit on Friday night, European Council President Donald Tusk said.
British Prime Minister David Cameron said he had negotiated a deal to give Britain "special status in the EU" and he would recommend it to his cabinet on Saturday.
That will fire the starting gun for a fierce campaign for a referendum on Britain's future membership of the bloc expected to be held on June 23, with the outcome deeply uncertain.
Both men made their announcements on Twitter as leaders at a summit dinner reviewed an amended text that resolved outstanding disputes over welfare benefits for migrant workers from other EU countries and safeguards for Britain's financial services sector from euro zone regulation.
"Deal. Unanimous support for new settlement for #UKinEU," Tusk's message said.
The agreement delivered victory to Cameron on several of the key demands on which he chose to fight for what he called "a new settlement" with Europe.
He won a commitment to change the bloc's governing treaties in future to recognize that Britain was not bound to any political union and would have safeguards against financial regulation being imposed on the City of London by the euro zone.
Cameron earlier postponed a planned cabinet meeting to stay on in Brussels and work for a deal he can sell to skeptical voters, who are almost evenly split over whether to stay in the EU according to opinion polls.
After all-night negotiations followed by a long day of private meetings to try to whittle down remaining differences, Tusk and European Commission President Jean-Claude Juncker put an amended clean text on the dinner table and the leaders quickly indicated their acceptance.
Earlier, a plenary session to review progress was postponed several times - from a late "English breakfast" to an "English lunch" and again till dinner at 8 p.m. (1900 GMT) - and leaders were asked to book hotel rooms for an extra night in Brussels.
Facing an uphill political battle at home, Cameron was concerned to show Britons that he had won concessions that he believes can reduce an influx of EU migrant workers and keep Britain out of any future political integration.
In hours of wrangling with central and east European countries that provide many of Britain's low-paid immigrant workers, he secured the right to curb in-work benefits for up to four years and scale back child benefit for workers whose children remain abroad.
European Parliament President Martin Schulz, whose assembly will have to pass legislation to implement concessions to Britain on benefit curbs, criticized some countries for trying to link demands on Europe's refugee crisis to the British deal.
He appeared to be referring to Greece, which had said it could block the entire deal unless it got its way on a dispute with Slovenia over border controls to curb the flow of migrants.
East European countries sought to restrict Cameron's welfare cuts to new arrivals rather than the more than 1 million European migrant workers already in the UK. In the end, both sides emerged with something to show for their negotiations.
A compromise that appeared largely favorable to Britain was found for French concerns about differential treatment for London banks outside the euro zone as well as Belgian grumbles about Britain setting a precedent for states to snub EU integration.
The stakes are high for both Britain and the EU, with opinion polls showing voters almost evenly split.
The risks of Cameron's strategy were highlighted on Friday when an opinion poll showed the campaign to leave the bloc had a two-percent lead with 36 percent support. The TNS poll showed 34 percent of British voters wanted to stay in the bloc, 7 percent would not vote and 23 percent were undecided.
Cameron was keen to show British voters he was fighting hard to secure a deal which he has called "the best of both worlds".
Britain is already the EU's most semi-detached member, having opted out of joining the euro single currency, the Schengen zone of passport-free travel and many areas of police and judicial cooperation.
Many leaders said they felt they were at a historic turning point for European integration.
No country has ever voted to leave the Union. Britain is the EU's second-largest economy and one of its two permanent members on the UN Security Council. Its exit would end the vision of the EU as the natural home for European democracies and reverse the continent's post-World War Two march toward "ever closer union".
Belgium, the most federalist of EU members, was pressing for a clause to ensure the deal with Britain would automatically cease to exist in case of a vote to leave - to make sure there was no possibility of a second renegotiation.
The issue has divided Cameron's Conservative Party for decades, crippling his 1990s predecessor John Major and bringing down his hero Margaret Thatcher.
Some Conservatives have criticized the reforms he is negotiating in Brussels as trivial, although most senior party figures are likely to join him in campaigning to stay in if he wins the concessions he is seeking.
Before even the final deal with Brussels was done, the BBC said his friend and justice minister Michael Gove would declare his intention to campaign to leave the European Union.
Britain's largely eurosceptic press depicted Cameron as begging or pleading, the Daily Mail describing him as "rattled".
"Shambles as embattled PM's deal is watered down," a front-page headline read over a picture of an anxious-looking Cameron.
(Additional reporting by Gabriela Baczynska, Elizabeth Piper, Alissa de Carbonnel, Francesco Guarascio, Paul Carrel, Andreas Rinke, Robert-Jan Bartunek, Philip Blenkinsop, Tom Koerkemeier, Jan Strupczewski, Alastair Macdonald and Robin Emmott in Brussels, Jason Hovet in Prague, Costas Pitas in London and Renee Maltezou in Athens; Editing by Andrew Roche and Alastair Macdonald)