The UK and the EU have agreed a “flexible extension” of Brexit until 31 October, European Council president Donald Tusk has said.
Speaking after five hours of talks at an EU summit in Brussels, Mr Tusk said his “message to British friends” was “please do not waste this time”.
Theresa May said the UK would still aim to leave the EU as soon as possible.
Irish Taoiseach Leo Varadkar said the UK must now hold European elections in May, or leave on 1 June without a deal.
The PM had earlier told leaders she wanted to move the UK’s exit date from this Friday to 30 June, with the option of moving that date forward again if she can get her withdrawal agreement ratified by Parliament.
Mr Tusk emerged from the talks – and a subsequent meeting with Mrs May – to address reporters at a news conference at 02:15 local time (01:15 BST).
He said: “The course of action will be entirely in the UK’s hands: they can still ratify the withdrawal agreement, in which case the extension can be terminated.”
He said the UK could also rethink its strategy or choose to revoke Article 50 and “cancel Brexit altogether”.
“Let me finish with a message to our British friends: This extension is as flexible as I expected, and a little bit shorter than I expected, but it’s still enough to find the best possible solution.
“Please do not waste this time.”
European Commission President Jean-Claude Juncker said: “There will probably be a European election in the UK – that might seem a bit odd, but rules are rules and we must respect European law and then we will see what happens.”
Mrs May then spoke to reporters at 02:45 local time (01:45 BST) and said that, should Parliament ratify the withdrawal agreement in the first three weeks of May, the country would then not have to participate in European elections.
The PM said that, until her deal is approved, the UK “will continue to hold full membership rights and obligations”.
She added: “I know there is huge frustration from many people that I have had to request this extension.
“I sincerely regret the fact that I have not yet been able to persuade Parliament.”
The EU had been split over the length of delay to offer the UK and by law they had to reach a unanimous decision.
Mr Varadkar said the EU “will take stock” of the situation at the regular EU summit in June.
The BBC’s Katya Adler said that the extension to 31 October was an indication that French President Emmanuel Macron had “won the day” as his was the most hardline voice in the room.
Malta’s prime minister Joseph Muscat said the 31 October deadline was “sensible” as it “gives time to UK to finally choose its way”.
The new deadline is one day before the next European Commission President – the successor to Jean-Claude Juncker – takes office.
Mrs May was called back into the summit after EU leaders had talked for five hours to find their compromise solution.
Before that, Mrs May had given a one-hour presentation putting forward her argument for the extension date to be 30 June.
This was the second time Mrs May has gone to the EU to ask for a Brexit extension.
So far, MPs have rejected the withdrawal agreement Mrs May reached with other European leaders last year and the House of Commons has also voted against leaving without a deal.