MOSCOW — Russia and Turkey agreed Thursday to a cease-fire in Syria's ravaged Idlib province, the latest agreement on a cessation of hostilities following an especially brutal campaign by Russian-backed Syrian forces against Turkish-supported rebel groups in the region.

The announcement, at a Moscow news conference held by Russian President Vladimir Putin and his Turkish counterpart, Recep Tayyip Erdogan, was made after nearly six hours of talks between the two leaders that came as the risk of direct military confrontation between Russia and Turkey increased sharply in the past week.

The cease-fire was set to go into effect at midnight.

The agreement stated that joint Turkish-Russian patrols will begin March 15 along a section of the M4 highway, one of Syria’s most important trading routes. No mention was made, however, of another keenly contested highway, the M5, which has been the focus of the worst clashes of the past week.

The northwestern corner of Syria, which consists of Idlib province and surrounding areas, borders Turkey and is home to thousands of rebel fighters and more than 3 million civilians, the United Nations says. The pocket is mainly held by the Islamist group Hayat Tahrir al-Sham, as well as Turkish-backed rebel groups. Tensions escalated sharply in January after the Russian-backed Syrian forces intensified their offensive in this last opposition stronghold.

The Syrian government’s months-long offensive to wrest control of Idlib has caused a major humanitarian crisis, as hundreds of thousands have fled their homes and headed toward Turkey, which has long since closed its border to refugees. Ankara, meanwhile, has deployed thousands of troops to Syria to prevent President Bashar al-Assad’s forces from defeating rebel groups, including Turkish-backed factions, and consolidating government control in Idlib and nearby areas.

“Turkey reserves the right to respond to any attack by the [Syrian] regime’s troops,” Erdogan said Thursday, adding: “Our task is to prevent the humanitarian situation in this region from sliding down.”

Since Feb. 3, 58 Turkish soldiers have been killed by Syrian airstrikes and ground attacks, data from the Turkish Defense Ministry shows, including two killed Thursday after the talks began. Erdogan said Turkey has killed more than 3,200 Syrian soldiers, in addition to destroying tanks, weaponry, air power and ammunition depots.

The Idlib tensions reached a boiling point last week when an airstrike blamed on the Syrian government killed 36 Turkish soldiers. Turkey retaliated by shooting down three Syrian warplanes in a week and inflicted heavy losses on ground forces.

But for the first time since the Idlib fighting erupted last year, Russia refrained from intervening on Syria’s behalf. Turkish and Russian forces have avoided direct conflict in an effort not to fracture ties between Moscow and Ankara.

“We need to discuss everything so that nothing like this will ever happen again and so that it won’t ruin Russian-Turkish relations, which both you and I respect and cherish,” Putin told Erdogan on Thursday in a portion of their one-on-one meeting that was broadcast on Russian state television.

Turkey has justified its intervention, in part, by saying it is aimed at preventing a bloodbath in Idlib. Hundreds of civilians have been killed in the region in recent weeks in what human rights groups describe as indiscriminate air and artillery strikes that frequently target civilian areas. One of Ankara’s main interests in stemming the violence is stopping a wave of refugees at its border — Turkey already has more than 3 million displaced Syrians, hosting more people displaced by the war than any other country.

Previous talks on Syria between Putin and Erdogan have produced stopgap measures but nothing sustainable. In the days leading up to Thursday’s face-to-face, analysts predicted that any new deal would be no different.

Moscow has accused Ankara of violating a 2018 cease-fire deal that established a demilitarized zone and of using artillery strikes and attack drones to defend rebel forces, including groups Russia and Syria consider terrorist organizations. Russia also has been accused of violating truce arrangements.

“It’s important . . . that we have a real cease-fire, a cease-fire that actually stops movement on the ground, not the phony cease-fires we have seen in Idlib,” James F. Jeffrey, the U.S. envoy to Syria, said Tuesday. He added that while Russia rebuffs accusations of cease-fire breaches by claiming it is targeting terrorists, it considers anyone opposed to Assad, including civilians, terrorists.

U.S. officials in the past few days, including Secretary of State Mike Pompeo on Thursday, have offered support to Turkey in the wake of the escalation in Idlib and underscored the risks of striking agreements with Russia.

Turkey, a NATO member, convened an emergency meeting of the alliance’s ambassadors last week, stopping short of an attempt to invoke NATO’s all-for-one, one-for-all mutual defense pact but still drawing the group into the increasingly tense situation.

Dadouch reported from Beirut. Liz Sly in Beirut contributed to this report.