Live updates: follow the latest news on Russia-Ukraine
The deal announced shortly before midnight by European Council President Charles Michel came after hours of negotiations in Brussels on a sixth package of EU sanctions against Russia.
Oil delivered by pipeline will initially be exempt from the ban, in a major concession to Hungary and its outspoken Prime Minister Viktor Orban, who had for weeks held EU sanctions in limbo.
Other countries will be granted grace periods to find alternatives to Russian oil, after landlocked, pipeline-reliant nations such as Slovakia and the Czech Republic asked for special treatment under the deal.
The compromise agreed to by the EU’s 27 leaders will need to be converted into a legal document and signed by officials from each country. It will then be phased in over a period of months.
But the political agreement sets a “very precise framework” for a final sanctions package, and a legal text will follow as soon as possible, an EU official said.
The long-awaited unity came hours after Ukrainian President Volodymyr Zelenskyy urged the EU to show unity and agree to the embargo on oil, at a time when Russia is probing for cracks in the western alliance.
Leaders then agreed a common position on Ukraine over talks that stretched through dinner and deep into the evening. Debates went beyond energy and covered food security and frozen Russian assets, an official said.
The sixth sanctions package will also isolate more Russian banks and add to the list of prominent political and military figures covered by EU sanctions. European companies will be banned from insuring Russian shipping.
Leaders agreed in principle to make €9 billion ($9.7bn) available to fill Ukraine’s financial gaps. But Mr Zelenskyy’s request for Ukraine to be admitted to the EU will be delayed until the next European Council summit in June, an EU official familiar with the conclusions said.
“We need political leadership in these extraordinarily difficult times,” Mr Michel said in a media briefing shortly before 1am Brussels time.
“More than ever it’s important to show that we are able to be strong, that we are able to be firm, that we are able to be tough in order to defend our values.
Mr Orban had kept the talks in suspense by announcing when he arrived for Monday’s summit that no compromise was on the table, and criticising the European Commission for its haste in unveiling the embargo.
But diplomats defended the eventual compromise by saying that even the revised package would cut off two thirds of Russia’s oil exports to Europe.
EU sources said the exemption for pipeline oil would be revisited as soon as possible, with Ukraine urging Europe to go further by cutting off Russian natural gas.
“Why can Russia still earn almost €1 billion a day by selling energy resources?” Mr Zelenskyy asked leaders in a 15-minute address earlier on Monday.
Mr Orban also won assurances that Hungary's neighbours would step in if oil supplies run dry, a response to mutterings from Ukrainian politicians about blocking the 4,000-kilometre Druzhba pipeline on its way from Russia to Hungary via Ukraine. Slovakia had sought similar promises.
One back-up option is for Hungary to receive oil through a pipeline from Croatia, although it will still need to adapt its refineries to handle crude from suppliers other than Russia. EU officials did not say who would pay for these upgrades.
Senior officials said 90 per cent of Russian crude exports could end up coming to a halt, after Poland and Germany offered assurances that they would press ahead with ditching Russian oil despite in theory being eligible for the pipeline exemption.
The remaining 10 per cent travels through Druzhba’s southern arm through Hungary and Slovakia, but phasing out the rest is still a “big step forward,” said European Commission president Ursula von der Leyen.
Ms von der Leyen had proposed a ban on all Russian oil on May 4, but ut the sixth package became bogged down in negotiations after the first five passed with almost unprecedented speed.
Mr Orban, long a thorn in the side of the EU leadership, had said that an oil ban for Hungary – which obtains 65 per cent of its oil from Russia – would amount to dropping an atomic bomb on its economy.
He had been the subject of intensive lobbying from Ms von der Leyen, French President Emmanuel Macron and, shortly before the summit, fellow landlocked-country leader Eduard Heger of Slovakia.
Mr Zelenskyy, who publicly admonished Mr Orban in his last appearance before the council in March, this time made a plea for unity as Russia raises the pressure on European countries to lift sanctions.
“Russia wants to see … not a united European Union, but 27 separate states, 27 fragments that cannot be put together,” he said.
Ukraine and some member states want the EU to keep pushing by discussing a ban on Russian gas. But some leaders believe it is time to take a pause after the oil discussions pushed Europe towards the limit of what is willing to do to help Ukraine.
Russian energy giant Gazprom announced it would cut natural gas supplies to Dutch trader GasTerra on Tuesday and it is considering cutting off Denmark. It has already turned the taps off to Bulgaria, Poland and Finland.
Latvian Prime Minister Arturs Krisjanis Karins said diplomats were “forgetting the big picture” and getting bogged down in details at a time when Russian missiles continue to pound Ukraine.
“It's only money, the Ukrainians are paying with their lives,” Mr Karins said. “We can and we must support them, if only out of self-interest because only when Russia is defeated can we in Europe feel safe.”
The Czech and Slovak governments had sought transition periods to step away from Russian oil, diplomats said, but distanced themselves from Mr Orban’s bombastic rhetoric.
The EU eventually “managed to stay united” and remained strong in supporting Ukraine despite concerns about western unity crumbling, said Lithuanian President Gitanas Nauseda after the deal was reached.
“Step by step Europe will achieve full energy independence. Russia will end up completely isolated."
Funding for Ukraine’s rebuilding will be linked to reforms of its judiciary to fight corruption, Ms von der Leyen said, one of the stepping stones to meeting the criteria for EU membership.
The focus of the two-day summit will switch to food security and defence on Tuesday. African Union representative and Senegalese President Macky Sall will address leaders over the emerging hunger crisis resulting from blocked food exports from Ukraine.