British Prime Minister Theresa May has accepted an offer from leaders of the remaining 27 EU countries to delay the deadline for Brexit to 31 October.
The deal struck during late night talks in Brussels means that, if London remains in the EU after 22 May, British voters will have to take part in European elections - or crash out on 1 June.
Mrs May said she would now keep working to get her withdrawal agreement approved by parliament to ensure an orderly split, saying her goal was to leave "as soon as possible".
The other 27 EU leaders met without Mrs May over dinner to thrash out what European Council president Donald Tusk called "a flexible extension until 31 October".
Mrs May later returned to agree the new deadline.
She addressed the House of Commons today as her officials met for further talks with the Labour Party to try to find a way through the political deadlock.
Without a postponement, Britain would have ended its 46-year membership of the EU tomorrow, risking economic chaos on both sides of the English Channel.
Mr Tusk had proposed a year-long delay, but said: "It's still enough to find the best possible solution. Please do not waste this time."
He suggested Mrs May's government now had time to ratify the deal agreed with EU leaders in November, to rethink its approach or to stop the entire Brexit process.
#EUCO on #Brexit:— European Commission 🇪🇺 (@EU_Commission) April 11, 2019
-Extension of article 50 granted until 31 October.
-UK to organise #EUelections2019 if Withdrawal agreement not ratified by 22 May.
-No reopening of the Withdrawal Agreement.
-Principle of sincere cooperation will apply to UK. https://t.co/WtzR97QDcO pic.twitter.com/4JFouDo1L9
- Brexit: What happens next?
- What you need to know about latest Brexit extension
- More Brexit stories
- EU summit outcome in Brussels was, as usual, a compromise
The summit conclusions say Britain must hold European elections set for 23 May or if "the United Kingdom fails to live up to this obligation, the withdrawal will take place on 1 June 2019".
Britain has already started planning for the polls, but Mrs May told reporters that she hoped she could still get her deal agreed by 22 May and avoid taking part.
"The EU have agreed that the extension can be terminated when the Withdrawal Agreement has been ratified," she said.
The summit was more tense than expected, with France's President Emmanuel Macron the strongest voice opposing a long extension as the talks stretched from early evening to early this morning.
With backing from Belgium, Austria and some smaller EU states, he pushed to limit the delay to only few weeks and demanded guarantees that London would not interfere in EU business during that time.
But most leaders backed the longer plan, including German Chancellor Angela Merkel, and the French had to settle a review of the delay at a pre-planned EU summit on 20-21 June.
Mr Macron said afterwards this was the "best possible compromise", which "made it possible to preserve the unity" of the other 27 EU states.
"The October 31 deadline protects us" because it is "a key date, before the installation of a new European Commission", he said.
Mrs May left the group after giving what one official said was a "solid" presentation of her case, but was kept up to date by Mr Tusk, who met her before, during and after the discussions.
Taoiseach Leo Varadkar said the European Union "is not a prison (and) nobody has to stay, but it's also a home and we're not going to kick anybody out either".
Asked if he thought the extension was long enough, he said: "I really hope so. The European Union is imperfect, we took several hours here to make a decision, there were different views, we had to bounce things around for five or six hours this evening but we did make a decision.
"It's three years since the referendum now and we really think it's time for the United Kingdom to make a decision, so we're giving them a very long period of time now to make a decision."
Mr Varadkar said there was "genuine concern" about the UK remaining in the EU for an extended period of time while it intends to leave in case it disrupts EU business.
"Quite frankly that is already the case, with Ukip and (Nigel) Farage," he added.
Elsewhere, US President Donald Trump responded to the postponement by saying the EU was being hard on Britain.
"Too bad that the European Union is being so tough on the United Kingdom and Brexit," he tweeted.
Too bad that the European Union is being so tough on the United Kingdom and Brexit. The E.U. is likewise a brutal trading partner with the United States, which will change. Sometimes in life you have to let people breathe before it all comes back to bite you!— Donald J. Trump (@realDonaldTrump) April 11, 2019
Mrs May is under intense pressure from hardline Brexit supporters in her Conservative party not to compromise in her talks with the opposition Labour party.
Addressing MPs back home, who have rejected her withdrawal text three times, she said after the summit: "The choices we now face are stark and the timetable is clear.
"So we must now press on at pace with our efforts to reach a consensus on a deal that is in the national interest."
Mrs May had originally asked for a delay until 30 June, but EU leaders have already agreed one delay from 29 March to 12 April, and Mr Tusk had warned there is "little reason to believe" that MPs would ratify the Brexit deal within three months.
Labour leader Jeremy Corbyn wants Britain to commit to remaining within the EU customs union, an idea that Mrs May has previous rejected - but which many in Europe would be keen to accommodate.
There had been speculation about conditions imposed on any extension, amid concerns a semi-detached Britain might leverage in Brexit talks by intervening in choosing the next head of the European Commission or the next multi-year EU budget.
The summit conclusions make clear Britain was a full member of the EU until it left, but noted the "commitment by the United Kingdom to act in a constructive and responsible manner throughout the extension".
Minister for European Affairs Helen McEntee said the extension will allow Mrs May more time to decide on the UK's future without forcing a no-deal scenario.
Speaking on RTÉ's Morning Ireland, she said the EU was not trying to keep the UK in the union.
"This essentially gives a little bit more time. The EU was very clear, we're not trying to keep the UK within the European Union. At the same time, we're not going to force them out and we're not going to force a no deal. So it gives the Prime Minister that little bit more time to decide what it is that they want, the direction that they're going," she said.
Conservative MP Jacob Rees-Mogg said: "We should have left the European Union already and that's what we need to deliver on.
"The Conservative Party was elected on a mandate of leaving the customs union, leaving the single market, and the Prime Minister needs to remember the votes that she won in that election, that gave her her mandate.
"And the Labour Party said at the same time it was committed to implementing the result of the referendum, so I think we are in a difficult political situation."
The Vice President of the European Parliament has said she is dismayed at the ongoing division in British politics over the country’s decision to leave the EU.
Mairéad McGuinness said extremes on both sides of the argument seemed to be dominating political discourse and delaying the achievement of an agreed resolution.
Speaking in Co Mayo this afternoon, she said last night’s European Council discussions did not resolve the uncertainty around Brexit and that the situation was no clearer today than it was when the UK voted to leave the EU in June 2016.
But she said the European Parliament did not want to force a crisis.
She expressed hope that last night’s Brexit deadline extension would give the UK "a bit more breathing space" to allow political leaders to come to some agreement about the next stage in the process.