Diplomats said the agreement would clear the way for other elements of a sixth package of EU sanctions on Russia to take effect, including cutting Russia's biggest bank, SberbankSBMX.MM, from the SWIFT messaging system.
"Agreement to ban export of Russian oil to the EU," said European Council President Charles Michel in a tweet at the end of the first day of a two-day summit of the bloc's 27 leaders.
"This immediately covers more than 2/3 of oil imports from Russia, cutting a huge source of financing for its war machine. Maximum pressure on Russia to end the war," he said.
Two thirds of the Russian oil imported by the EU comes via tanker and one-third by the Druzhba pipeline. The embargo on seaborne oil imports would therefore apply to 2/3 of all oil imported from Russia.
The embargo would encompass 90 percent of all imports from Russia once Poland and Germany, which are also connected to the pipeline, stop buying it by the end of the year.
The remaining 10 percent will be temporarily exempt from the embargo so that landlocked Hungary, which was the main holdout for a deal, along with Slovakia and the Czech Republic, which are all connected to the southern leg of the pipeline, has access which it cannot easily replace.
Budapest also appeared to have won reassurances from other leaders that emergency measures would apply "in case of sudden interruptions of supply" following concerns raised by Prime Minister Viktor Orban about risks posed to the Russian oil pipeline that runs through Ukraine to Hungary.
The ban on oil imports to EU countries will apply to Russian crude that is delivered by shipments.
It was not immediately clear how member states that receive oil from tankers would be compensated for the higher cost compared with those that will keep the pipelines open.
Zelinskiy chides Eu leaders
Earlier, Ukrainian President Volodymyr Zelenskiy chastised the EU leaders in a video address for being too soft on Moscow as an agreement on an oil embargo still appeared elusive.
"Why are you dependent on Russia, on their pressure, and not vice-versa? Russia must be dependent on you. Why can Russia still earn almost a billion euros a day by selling energy?" Zelenskiy said.
The EU has rolled out five rounds of sanctions since Russia attacked Ukraine in February, demonstrating uncharacteristic speed and unity given the complexity of the measures.
But the haggling over an oil import ban exposed a struggle to widen sanctions as the economic risk for Europe grows, because so many countries depend on Russian crude.
Dutch Prime Minister Mark Rutte said as he left the Brussels talks that he had been surprised by the turn of events.
"At the beginning of the evening I wasn't at all hopeful, but at 11 p.m. or so, it was done," he said, adding that technical details still unresolved should not be difficult.
The summit also brought political backing for a package of EU loans worth 9 billion euros ($9.7 billion), with a small component of grants to cover part of the interest, for Ukraine to keep its government going and pay wages for about two months.
Leaders also backed the creation of an international fund to rebuild Ukraine after the war, with details to be decided later.
On Tuesday the leaders will pledge to accelerate work to help Ukraine move its grain out of the country to global buyers via rail and truck because the Russian navy is blocking the usual sea routes, and to take steps to more quickly become independent of Russian energy.