BEIRUT, Lebanon — The last busloads of insurgents left the battered Old City of Homs in central Syria on Friday, under a deal handing over the longtime bastion of rebellion to the government after nearly two years of blockade and bombardment.

Residents trickled in to gaze in shock at blocks of bomb-bitten homes. Some wheeled bicycles or carried gawking children on their shoulders, while others wept and fled in grief from camera operators, in scenes captured on state television and in snapshots that flooded social media. Soldiers giving the victory sign planted government flags in a square at the edge of the district.

Across the country, in Aleppo Province in the north, rebels allowed a convoy of trucks delivering humanitarian aid into villages they had long blockaded, fulfilling their end of the deal so that the government would allow the last fighters out of Homs.

In carrying out the complex pact, the government and its armed opponents displayed a new ability to talk and coordinate with one another. Officials said the effort could pave the way for an agreement to evacuate fighters from Hay al-Waer, the last insurgent-held district on the outskirts of Homs, and to end the government’s blockade of about 200,000 people there, half of them displaced from elsewhere.