Turkish-backed Free Syrian Army members celebrate Sunday in Afrin, Syria. (Khalil Ashawi/Reuters)

Turkish forces and their rebel allies wrested a key stronghold from Kurdish militants in northern Syria on Sunday, military and government officials said, marking the potential end of a weeks-long battle in which scores of civilians were killed.

Gun-toting rebels backed by Turkish troops and tanks swept through the city of Afrin early Sunday, with the fighters posting photos and videos from outside municipal buildings and in public squares. The Kurdish YPG militia, which controlled Afrin, had withdrawn from the area just hours before amid fierce air and artillery strikes.

Turkey says the Syrian Kurdish militants are linked to Kurdish guerrillas attacking Turkish troops at home and has portrayed its offensive — Operation Olive Branch — as critical to its national security.

“In Afrin’s center, it is no longer the rags of the terror organization that are waving but rather the symbols of peace and security,” Turkish President Recep Tayyip Erdogan said in a speech Sunday.

Erdogan said Turkish troops and rebel fighters had gained “total control” of the city.

“Many of the terrorists had turned tail and run away already,” he said.

The offensive, however, underscored the extent to which foreign powers have come to dominate the Syrian battlefield, using proxies to wage local conflicts and gain leverage on the international stage.

The battle for Afrin comes as part of a wider struggle between Turkey and Kurdish separatists in the region. It has also ripped through the U.S.-Turkish alliance, which has frayed since U.S. troops began allying with Syrian Kurds to fight the Islamic State militant group.

The apparent victory for Turkey on Sunday was a stunning blow to the YPG, which has sought to consolidate control over Kurdish areas of Syria in the hopes of forging an autonomous state.

Amid Syria’s descent into civil war, Kurds in the country have seized territory and formed their own militias and administration. The YPG — or People’s Protection Units — has controlled majority-Kurdish Afrin since 2012. The group also holds swaths of territory in northern and eastern Syria, much of which was captured in the fight against the Islamic State.

Turkish forces and Free Syrian Army members pose with their flags Sunday in Afrin, Syria. (Khalil Ashawi/Reuters)

A senior Kurdish official from Afrin, Hevi Mustafa, said Sunday that Kurds there “face catastrophic challenges.”

In a telephone interview from outside the city, she said the YPG withdrew “to begin a new stage of resistance.”

“Our military forces will continue to fight,” she said. “But there are large numbers of civilians in need of urgent help.”

Another Kurdish official, Othman Sheikh Issa, said Kurdish fighters would resort to guerrilla warfare to target Turkish troops and their proxies.

Tens of thousands of civilians had either fled Afrin or were displaced inside the enclave in recent weeks, the United Nations said. The world body also said local authorities had prevented many residents from leaving, even as a blitz of airstrikes forced civilians into basements in the city.

It was unclear Sunday how many civilians remained — or whether the thousands who had fled would be able to return.

Mohammad Atarib, a fighter with the Turkish-backed Free Syrian Army, said he encountered residents when he entered Afrin on Sunday.

“At first they were scared,” he said. “But then they gave us water and food.”

FSA forces were widely criticized after footage emerged that appeared to show the fighters abusing and executing civilians during the operation, and photos captured rebels looting shops.

In a statement Sunday, Turkey’s military said its forces and allied fighters were sweeping the city for mines and homemade bombs. The military also posted footage of a soldier holding a Turkish flag over a local government building. Another image showed a soldier raising and saluting a Turkish flag over the city.

“We are urging the Turkish army and FSA to have no military presence in the city” as locals seek to rebuild, a Kurdish official from the Syrian city of Hasakah said in an interview Sunday.

Abdel Aziz Timo, who heads a Syrian Kurdish coalition opposed to the YPG, said Turkey should rein in abuses by the FSA.

“There are some members who want to take revenge against the YPG,” he said in an interview in Gaziantep. “The first thing they need to do is bring security and stability.”