ISTANBUL — Militias loyal to the Syrian government swept into the northwestern enclave of Afrin on Thursday in support of Kurdish militias, reclaiming the territory and stealing a march on Turkish forces that have been battling toward the city for nearly a month.
Television broadcasts and social media postings showed crowds celebrating in the main square of the city of Afrin, waving flags and holding posters of President Bashar al-Assad of Syria and the Kurdish militant leader Abdullah Ocalan, who is imprisoned in Turkey on terrorism charges.
The entry into Afrin of forces loyal to Mr. Assad — the result of a deal between the Syrian government and Kurdish militias, with the backing of Iran and Russia — has harmed Turkey’s ambitions in Syria. It is one of many setbacks that Turkey’s president, Recep Tayyip Erdogan, has experienced throughout the seven-year Syrian civil war.
“It’s not something Turkey is happy with at all,” said Michael Stephens, who studies the Middle East at the Royal United Services Institute in London. “It limits Turkish strategic options.” Turkey has made it clear that if attacked by pro-government forces, its forces will strike back, he said.
Turkey began its incursion into Afrin a month ago, saying it wanted to clear the enclave of Kurdish militias, which it says are affiliated with Mr. Ocalan’s Kurdistan Workers’ Party, or P.K.K., which has long waged a separatist insurgency in Turkey.
But Turkish forces have struggled to make headway against the well-prepared Kurdish fighters. In a month of fighting, Turkish forces have lost 32 soldiers. They have taken several dozen villages along the Turkish border, but have yet to reach the main cities.
The Syrian government has opposed the Turkish action from the start, accusing it of a breach of Syrian sovereignty, but Russia, which controls Syrian airspace, opened airspace to Turkish war planes.
Syrian and Kurdish officials suggested from the start that the Syrian government could move in to help the Kurdish forces.
Ibrahim Hamidi, a Syrian journalist for the London-based Arab newspaper Asharq al-Awsat, reported on Wednesday that senior Russian and Syrian government officials met with Saban Hamo, the leader of the Kurdish People’s Protection Units, or Y.P.G., in the city of Aleppo to work out a deal.
The head of the Syrian government forces’ security committee, Brig. Gen. Malek Alia, attended the meeting along with the head of the Russian Army Reconciliation Center in northern Syria, Mr. Hamidi reported.
In separate meetings on the opposing side, American and Turkish officials gathered to work out a solution for the town of Manbij, he added. Mr. Erdogan has demanded that Y.P.G. forces also be removed from Manbij, where United States forces have a base and work with the Y.P.G. in its fight against the Islamic State. Mr. Erdogan has threatened to expand the Afrin operation to attack the town, straining United States-Turkish relations.
The Kurdish militias have welcomed Syrian government support in their fight against Turkish forces but risk losing their autonomy. Brusk Haska, a military official in the Y.P.G., said in an electronic message earlier this week that they would accept any help for their forces in Afrin.
“We welcome any force that comes to protect Afrin and the civilians from the Turks NATO aggression,” he wrote. “We are part of Syria and not part of the regime, we still have our own administration but we welcome any party coming to protect us.”
Turkish officials insisted their operation would continue and expressed skepticism about the Syrian government’s intentions.
“If they enter to clean P.K.K./Y.P.G. out, there is no problem,” Turkey’s foreign minister, Mevlut Cavusoglu, said, using the acronyms for the Turkish and Syrian Kurdish groups. “But if the regime enters to protect the Y.P.G. there, no one can stop us.”
Turkey’s defense minister, Nurettin Canikli, said on Thursday that he doubted that the pro-Syrian government militias advancing to Afrin were capable of subduing the Kurdish militias, which is Turkey’s goal.
“The armed units allegedly sent to Afrin by the Syrian regime have no capacity to change the result of the antiterror struggle we have been waging in that region, and they never will have,” the Anadolu news agency reported Mr. Canikli’s telling journalists during a military ceremony in the province of Kayseri. “Whoever sides with terror will become our target.”
Turkish analysts pointed out that Russia was manipulating the players from behind the scenes. “Russia is not outside the process, rather it is at the center of the deal,” said Kerim Has, a lecturer at Moscow State University. He said that Russia was using the Turkish assault on Afrin to force the Syrian government and the Kurds to work together. “The stick is Turkey and now the P.Y.D. will sit around the table on Moscow’s terms,” Mr. Has said, using the acronym for the political arm of the Y.P.G.
Mr. Erdogan has defended the Afrin incursion as necessary for Turkey’s national security, but he has also promised that it would allow some of the 3.5 million Syrian refugees in Turkey to return home. His aim has been to forge a buffer zone along Turkey’s border that would be cleared of Kurdish militants and that could be occupied by Syrian Sunni forces who have been in the forefront of the opposition to Mr. Assad.
Many Free Syrian Army fighters joined the operation, hoping to be able to return to their homes in villages in northern Syria. They have, however, suffered scores of casualties and are no closer to that aim.
Yet the proposal for the Syrian government to take control of Afrin could also offer Mr. Erdogan a way out, as the situation becomes increasingly complicated.
“He can claim victory or some kind of strategic gain if Assad works against P.Y.D. control in the area,” said Mr. Stephens, the analyst in London. “Then he can withdraw in respect for Syrian sovereignty whilst saying to his public that a critical border security issue has been dealt with.”
He added: “Turkey’s strategic position was always quite weak, and they are playing for tactical gain.”