Russia, which halted shipments four days earlier, wanted the written deal to renew gas shipments that it suspended amid a bitter contract dispute with Ukraine. The suspension was seen by many as yet another attempt by Moscow to reassert its clout over Western-leaning former Soviet republics.
Russia said it needed European Union monitors deployed to Ukraine to prevent the country from stealing Russian gas intended for Europe. Ukraine hotly denied the claims.
Czech Prime Minister Mirek Topolanek, whose country holds the rotating EU presidency, shuttled between Moscow and Kiev on Saturday to mediate the deal. He finally persuaded Ukraine to accept the monitoring pact during marathon talks that dragged past midnight.
"Nothing prevents Russia now from resuming gas supplies," Mr. Topolanek said after Ukrainian officials endorsed the deal.
"We once again have shown our goodwill," said Ukraine's prime minister, Yulia Tymoshenko.
Russian Prime Minister Vladimir Putin promised that Moscow will resume gas shipments once the deal is signed and monitors are in place.
Officials didn't say when exactly Russia will restart shipments, but EU officials warned it would take several days to repressurize the pipeline network and deliver gas to Europe.
Russia supplies about one-quarter of the EU's natural gas, most of it shipped through Ukraine, and the disruption has come during a harsh winter. At least 11 people froze to death last week in Europe, including 10 in Poland, where temperatures have sunk to minus 13.
Mr. Topolanek talked with Mr. Putin for more than five hours at his suburban residence outside Moscow after visiting Ukraine the previous night. He then rushed back into the Ukrainian capital late Saturday to get Ukraine's approval.
"This agreement is very important for us," Mr. Topolanek said during the talks with Ukrainian officials. "We can't allow the entire energy system of Europe to collapse."