Syrian state media broadcast pictures of government forces raising flags over wrecked buildings as the rebels declared they had withdrawn from the city.

The rebels, who had held the city for more than a year, fought on for more than two weeks — longer than expected — against a far larger force and inflicted unaccustomed casualties on Hezbollah’s seasoned fighters, many of whom were buried around Lebanon.

But the situation inside Qusayr had grown desperate. Ammunition was running out, rebel reinforcements were unable to penetrate the government cordon around the city and with medical supplies dwindling, hundreds of wounded people could not be evacuated as Hezbollah fighters assaulted the city backed by heavy government airstrikes and artillery bombardment.

The retreat apparently followed an intervention by the United Nations, which had expressed concern about a humanitarian crisis in the city, especially after the government and Hezbollah refused to allow Red Crescent humanitarian workers into the city before the end of military operations.

A member of the Syrian National Coalition, the main exile opposition group, said on condition of anonymity that after mediation by the Lebanese politician Walid Jumblatt, United Nations officials relayed a message that Mr. Assad had agreed to allow the wounded to leave on condition that “armed gangs” leave Qusayr and hand over the city.

Quoting a Syrian security source, Reuters reported that the military and Hezbollah had left open a corridor allowing rebels to withdraw to the nearby village of Daba’a and toward the Lebanese border. Rebels in Daba’a and other villages near Qusayr remain dug in, using tunnels and storing food and supplies in underground command rooms that were seen by a reporter who recently visited the area.

The battle fit a pattern in which rebels hang on until the last minute and then announce a tactical withdrawal. Syrian forces have sometimes been unable to hold reclaimed territory. Rebels said some fighters were still in the city and Syrian state media said the military was sweeping the northern portion of Qusayr for militants.

The battle — the largest and most public intervention yet by Hezbollah, the Lebanese Shiite militant group allied with Mr. Assad — increased tensions throughout the region, pitting Hezbollah against mostly Sunni rebels from Qusayr as well as jihadists from Lebanon and other countries who had joined the battle.

Fighters and civilians around Qusayr used increasingly sectarian language during the battle, vowing revenge on Shiites in general and Hezbollah in particular. Hezbollah-controlled residential areas inside Lebanon were rocketed in attacks blamed on Syrian rebels or their Lebanese Sunni supporters, who also increased their attacks on Alawite supporters of Mr. Assad in the northern Lebanese city of Tripoli.

Sunni clerics issued decrees calling on their followers to rush to Qusayr to help, but the call proved more rhetoric than reality. Fighters and activists in Qusayr expressed confusion and despair on Wednesday.

“What happened to all the fighters who were on their way to Qusayr to support us?” said Ammar, an antigovernment activist who used only his first name for safety.

Another activist, Jad al-Yamani, who lost his brother in the battle, said from outskirts of the city, “Now I lost everything. I can no more return back to my town.”

Syrian media and military officers portrayed the development as a possible turning point in the conflict.

“He who controls Qusayr controls the center of the country and he who controls the center of the country controls the whole of Syria,” said Brig. Gen. Yahya Suleiman, speaking to Beirut-based Mayadeen television.

But rebels and opposition activists vowed to fight on. They said they had managed to evacuate some of the wounded, although there were fears of reprisals against those who remained.

“Yes my brothers, it is one round that we lost,” the Qusayr Coordinating Committee, an antigovernment group inside the city, posted on its Facebook page. “But war is a drawn out competition.”

It added that some fighters had remained behind to ensure the safety of civilians and wounded, saying, “We ask God to bless those militants who vowed to die on the soil of beloved Qusayr.”

Hwaida Saad and Hania Mourtada contributed reporting from Beirut, Lebanon, and an employee of the New York Times from Daba’a, Syria.