European Union leaders agreed unanimously on a package of measures aimed at keeping Britain in the 28-nation bloc at an extended summit this evening, 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 tomorrow.
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 23 June, 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 eurozone regulation.
The agreement delivered victory to Mr 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 recognise 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 eurozone.
Mr Cameron earlier postponed a planned cabinet meeting to stay on in Brussels and work for a deal he can sell to sceptical 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, Mr 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 8pm - and leaders were asked to book hotel rooms for an extra night in Brussels.
Facing an uphill political battle at home, Mr 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.
Deal. Unanimous support for new settlement for #UKinEU— Donald Tusk (@eucopresident) February 19, 2016
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.
Mr Cameron held talks earlier with the Taoiseach on the sidelines of the summit. Mr Cameron was apparently grateful for the staunch support that Enda Kenny has given him at the summit table.
The government is fearful about a British exit and there is also concern that Irish workers in the UK be exempt from any restriction on benefits for EU nationals that will emerge as part of any deal.
Yesterday the Irish Ambassador to Britain confirmed that the Government has held talks with London to ensure that Irish nationals living in Britain are not affected by any restrictions to benefits which may emerge as part of a deal to keep the UK in the EU at this week's summit in Brussels.
Speaking on Bloomberg television, Ambassador Dan Mulhall said Ireland's arrangements with the UK regarding Irish nationals working there were "separate" from Mr Cameron's efforts to restrict migration and limit benefits for those coming from Central and Eastern Europe.
Mr Mulhall said: "We will continue to discuss this issue because it's recognised on both sides this is a particular issue which is a little bit separate from migration from other European countries which is a more recent phenomenon for Britain."
He added: "The issue of Irish people living in the UK predates the EU by many years. We have of course discussed this issue with the British government, and depending on what the outcome of this whole process might be, obviously it is a matter that will have to be continued.
Greece has threatened not to sign off on the final conclusions of the summit, holding out for assurances EU states will not shut borders over the migrant crisis, government officials said.
"We can't agree unless there is a clear statement in the conclusions that there won't be any unilateral (migrant) action until the next summit. No borders should close until then," one of the officials said.
The official said Greek Prime Minister Alexis Tsipras has been given assurances by Germany and France that they would support his position.
Other countries have expressed differences, however.
Analysis: Europe Editor, Tony Connolly
No-one said it would be easy, nor did they predict it would be this hard.
While the expectations were that there would be some semi-artificial drama before EU leaders grudgingly gave David Cameron the concessions he needs to launch a referendum campaign to stay in the EU, in the end genuinely deep divisions remain.
Mr Cameron left the summit venue without speaking to reporters at 5am. He had held bilateral negotiations throughout the night with the French and Belgian leaders, and with the Czech prime minister. But stubborn differences were proving hard to resolve.
These mainly revolve around the benefits that EU nationals working in the UK can enjoy.
Mr Cameron wants an emergency brake on in-work benefits for EU migrants to be in place for up to 13 years. Central and eastern European leaders are insisting it is no more that five years.
There are other differences on the rules involving child benefit, and also on what privileges the UK should enjoy in the realm of financial regulation.
Mr Cameron urged fellow leaders to help him settle the UK's relationship with Europe for a generation. While they all want to help they do not want to make it easy for him.