BEIRUT — The first phase of a troubled population transfer concluded Friday as thousands of displaced Syrians were evacuated out of besieged areas and an agreement was reached to release hundreds of government detainees, government media and rebels said.

One of the largest population transfers in Syria's civil war had been tied to the fate of 26 hostages, including members of Qatar's ruling family, who had been held by a Shiite militia in Iraq and were released Friday. Qatar, which is the patron of some Syrian armed opposition groups, was a main negotiator of the deal. Iran, which backs Iraqi and Lebanese militias fighting in Syria, served as the other negotiator.

With the transfer completed Friday, the first phase of the population swap deal in Syria comes to an end. A total of 8,000 residents from the pro-government villages of Foua and Kfarya — besieged for two years amid Syria's bloody civil war by anti-government rebels— and nearly 3,000 evacuees from the rebel-held Zabadani, Madaya and surrounding areas, have left their homes.

Under the deal, an estimated 30,000 people will be transferred from their hometowns over 60 days, most of them from the pro-government villages in northern Idlib. It is one Syria's largest population transfers, which the opposition has described as "demographic engineering," and the first to involve a reciprocal population swap.

The Syrian military media arm said 46 buses carrying residents of Foua and Kfarya arrived in Jibreen, an Aleppo suburb, on Friday. Some 15 buses were carrying residents and rebels from the Zabadani area, which was besieged by the government, departed for Idlib.

Rebel spokesman Mohammed Abo Zayed, from Ahrar al-Sham group negotiating the deal, said the phase now ended with the agreement to release 500 detainees from government prisons who were expected to arrive in rebel areas in the next few hours. Another 250 detainees will be released in the next 10 days, he said.

Syrian media declared that armed groups no longer have any presence in the Zabadani and Madaya area, which government troops have entered after the evacuation. The Syrian military arm media said it had destroyed a tunnel linking the rebel areas.

Madaya and Zabadani, once summer resorts to Damascus, have been shattered under a crippling government siege. The two towns rebelled against Damascus' authority in 2011 when demonstrations swept through the country demanding the end of President Bashar Assad's rule. Residents were reduced to hunting rodents and eating the leaves off trees. Photos of children gaunt with hunger shocked the world but the siege remained.

In northern Syria, Foua and Kfraya, besieged by the rebels, lived under a steady hail of rockets and mortars. They were supplied with food and medical supplies through military airdrops.

The population transfer faced a number of snags, including a deadly explosion Saturday. The massive bombing struck as evacuees waited for over 30 hours to be transferred to Jibreen on Saturday, killing at least 130 people, most of them children and government supporters.

Rights groups have decried the agreement as a forcible displacement that is altering the country's demographics along political and sectarian lines.

In the last year alone, thousands of rebels, their families and supporters were forced to surrender in capitulation deals under intense government bombings and tight sieges across a number of towns and areas around the capital Damascus, as well as in Aleppo and Homs. Most of those have been transferred to rebel-held Idlib.

Syrian state TV al-Ikhbariya broadcast the arrival of several buses carrying hundreds of residents — to Jibreen, where the government set up a temporary shelter for them.

Relatives rushed to the buses to receive their loved ones, who had been stranded at an exchange point since Wednesday. Others frantically searched for missing relatives. Lebanese Al-Manar TV, affiliated with Syrian government ally Hezbollah, said at least 12 bodies of those killed in the Saturday explosion were among those arriving in Aleppo.

"Whoever knows anything about my kids, please let me know," a mother wept on al-Ikhbariya as she pleaded for information for her three kids missing since the explosion last week.

No one has claimed responsibility for the bloody explosion.

In pictures from rebel-held areas, one armed man could be seen hugging another after the buses arrived in rural Idlib.