BEIRUT — Turkey shot down two warplanes and inflicted heavy losses on ground forces in northwestern Syria on Sunday as the two countries edged closer to an all-out war.

The operation came in retaliation for an airstrike blamed on the Syrian government that killed 36 Turkish soldiers Thursday, Turkey’s Defense Ministry said. It followed weeks of Turkish threats to attack Syrian forces if they continued to advance toward the Turkish border, risking a new wave of refugees.

Armed Turkish drones struck military airports and loyalist bases deep in Syrian government-held territory as Turkish troops and allied rebels pushed forward to drive Syrian troops out of towns and villages they had recaptured from opposition forces in recent weeks.

Russia, Syria’s most important ally, refrained from intervening on its behalf for the first time since the Idlib fighting first erupted last year, suggesting an unwillingness by Moscow to allow the spiraling confrontation between Turkey and Syria jeopardize its relationship with Ankara or to escalate into a wider conflict with an important NATO member.

The situation on the ground was fluid, and the progress of the Turkish-backed offensive was difficult to assess. But it appeared the Turkish intervention had succeeded in halting, and in some places reversing, weeks of Syrian government advances into the last enclave of rebel-held territory that have sent nearly a million people fleeing toward Turkey for safety.

The United Nations has called the exodus the largest single displacement and one of the biggest humanitarian catastrophes of the nine-year Syrian war.

It also underscored the potential for the battle over this last sliver of rebel-held territory to spill beyond Syria. The United States, an important Turkish ally that maintains troops in eastern Syria, and Iran, which has contributed militias to the Syrian government’s war effort, are invested in the outcome.

An Iranian news outlet reported that 21 Iranians had been killed in Turkish attacks, and the Iranian-allied Lebanese militia Hezbollah buried five fighters in Beirut on Sunday, among eight killed in the battles.

Turkey’s Defense Ministry said warplanes had shot down two Syrian Su-54 fighter jets because they posed a threat to Turkey’s forces in the area. The Syrian pilots ejected and parachuted to safety behind government lines, the official Syrian news agency said.

Syria said it had downed three Turkish drones and warned that any aircraft flying over the area would be treated as hostile and shot down. “Turkish hostile acts will not succeed in saving terrorists from the strikes of the Syrian Arab Army,” SANA, the state news agency, quoted an unnamed military official as saying.

But Turkey appeared to have gained the upper hand in a battle that had seemed to be going Syria’s way.

Turkish Defense Minister Hulusi Akar said Turkey has killed 2,200 Syrian troops and destroyed large amounts of heavy weaponry, including 103 tanks, six air defense systems and 72 Howitzers and rocket launchers, since the fighting ticked up in recent days. Turkey said it had killed three senior Syrian generals in drone strikes.

Turkey has been escalating military operations against Syria since the 36 troops were killed. Officials formally announced the launch of Operation Spring Shield on Sunday.

The Turkish figures could not be independently verified. Danny Makki, a Syrian analyst who works in London and Damascus, said the operation appeared to have taken a heavy toll on the Syrian army, which relied on Russian air support and Iranian-backed militias for the progress it had made in recent weeks. He estimated that hundreds of Syrian troops have been killed in recent days.

“You’ve got Turkish troops pounding Syrian forces, inflicting mass casualties and taking out huge chunks of armor,” he said. “This is going to limit Syrian capabilities in Idlib and paralyze Syrian forces along the front lines.”

Resentment is growing among loyalist Syrians at Russia’s failure to come to the army’s rescue as its losses mount, Makki said. Almost no Russian warplanes have been seen over the area since the Turkish troops were killed Thursday.

“Over the past few days, Russia has done nothing in the face of an offensive that is rapidly destroying the capabilities of the Syrian army,” Makki said. Although it is not in Russia’s interest to see the Syrian army it has supported for years eroded by Turkey’s superior military, he said, “Russia’s central interests in Syria have been secured, whether political or economic, so Russia has nothing to gain by entering into an awkward and bloody confrontation with Turkey, which is a member of NATO.”

The sharp escalation followed weeks of building tension, during which Turkish President Recep Tayyip Erdogan threatened repeatedly to launch a military operation if the Syrian government didn’t by the end of February pull all of its troops back from the territory they had taken. The airstrike Thursday seemed to have hardened his resolve.

Turkey acted over the weekend to fulfill another recent threat, opening its borders for refugees and migrants who want to travel to Europe and triggering a flood of thousands toward its border with Greece, which closed all border crossings with Turkey on Sunday, trapping many hundreds in a no man’s land between the two countries.