The current deal between the two ex-Soviet countries expires Tuesday and ties between them have been shredded since Moscow annexed Crimea in 2014 and supported a separatist insurgency in eastern Ukraine.
Presidents Vladimir Putin and Volodymyr Zelensky discussed the deal by phone and congratulated each other ahead of New Year's celebrations, a sign that their relations could be on the mend.
The gas deal "creates a positive atmosphere for solving other bilateral problems," the Kremlin said in a statement.
About 18 percent of the European Union's annual natural gas consumption comes from Russia via Ukraine, which put pressure on EU officials to help broker the deal.
"Ukraine has signed a five-year transit contract," Zelensky announced in a late-night post on his Facebook page, nearly two weeks after a provisional deal was reached.
A wide range of documents and contracts were involved, and together formed "a package deal which has re-established the balance of interests," Alexei Miller, the boss of Russian gas giant Gazprom, was cited as saying in a statement.
The documents were signed after five days of non-stop talks.
Gazprom is expected to ship at least 65 billion cubic meters (2.3 trillion cubic feet) of natural gas via Ukraine next year, and at least 40 billion per year from 2021 to 2024, said Zelensky, from which Kiev would earn "more than seven billion dollars".
The agreement should prevent a repeat of so-called gas wars that previously disrupted supplies and in some years caused real energy problems in EU member states.
EU Commission vice president in charge of energy Maros Sevcovic called the deal "great news for Europe's energy security" on Twitter, while German Chancellor Angela Merkel thanked Russia and Ukraine.
"Continued gas transit via Ukraine... is a good and important signal for ensuring our European security of gas supply," she said.
Last year, Gazprom supplied Europe with 200.8 billion cubic metres of natural gas, about 40 percent of which passed through Ukraine for roughly three billion dollars in transit fees.
Russian President Vladimir Putin said earlier this month that Moscow wanted to keep some gas flowing through Ukraine, despite having built several pipelines to Europe since the current deal was agreed a decade ago.
The new agreement comes days after Gazprom paid $2.9 billion to Ukraine's Naftogaz to settle a long-running dispute over transit fees that had blocked the deal.
Meanwhile, Russia is pursuing work on the Nord Stream 2 project that is to be completed by the end of next year and would double gas shipments to Germany.
The United States has long opposed the 9.5-billion-euro ($10.6-billion) project and the US Senate voted last week to levy sanctions on companies working on it.
Washington believes the pipeline will give Russia too much influence over security and economic issues in western Europe.
Transit problems for Russian gas began after the fall of the Soviet Union when independent Ukraine took control of the pipeline infrastructure.
Several crises followed, with Russia using gas supplies to put pressure on Ukraine by cutting them repeatedly in 1992, 1993 and 1994.
The last gas crisis disrupted supplies to Europe in 2010.