Turkey and Russia announced a ceasefire in northern Syria on Thursday, agreeing to end weeks of heavy fighting that sparked a humanitarian disaster and raised fears of their armies clashing.

After more than six hours of talks in Moscow, Russian President Vladimir Putin and Turkish counterpart Recep Tayyip Erdogan agreed on the ceasefire to take effect from midnight on Friday.

The agreement will also create a security corridor along the key M4 highway in northern Syria, where Turkish and Russian forces will launch joint patrols from March 15.

The deal aims to put a stop to intense fighting in Idlib, the northwestern province of Syria where Ankara is battling Moscow-backed government forces.

Nearly a million civilians have fled their homes due to the violence and dozens of Turkish soldiers have been killed.

Putin told a joint press conference after the talks that the agreement would "serve as a good basis for ending fighting" in Idlib and for "stopping the suffering of the civilian population."

"The object is to avoid the humanitarian crisis getting worse," Erdogan said, though he added that Turkey reserved the right to "retaliate with all its strength against any attack" by Damascus.

The situation in Idlib, the last rebel stronghold in Syria's nine-year civil war, had become critical as Ankara for the first time launched a direct offensive against President Bashar al-Assad's forces.