(Reuters) - Warring sides in Syria have agreed with U.N. help on a deal to extricate one group of rebel fighters and another group of trapped villagers, sources familiar with the talks said on Thursday.

Civilians rush away from a site hit by what activists said was an airstrike by forces loyal to Syria's President Bashar al-Assad, in Kafruma village in the southern countryside of Idlib, Syria September 24, 2015. REUTERS/Khalil Ashawi

The deal was reached in talks backed by Iran, which supports the Syrian government, and Turkey, which supports the rebels. With a ceasefire in the areas holding, it offers a rare chance of success for foreign-brokered diplomacy in the intractable conflict.

It also comes as Russia shores up Damascus with increased military support, prompting the United States to talk with its Cold War foe.

Syria’s four-year civil war has killed an estimated 250,000 people and driven more than 11 million from their homes.

The agreement includes the withdrawal of rebel fighters holed up in the mostly government-held area near Lebanon, and the evacuation of civilians from two Shi’ite villages under rebel siege in Idlib province in northwest Syria, the sources said.

The office of U.N. Syria envoy Staffan de Mistura confirmed that there were positive developments in the talks, which were facilitated by the United Nations, but said it was up to the parties to say if there was a deal to announce.

One source close to talks on the government side, and another with knowledge of the negotiations, told Reuters that it would be implemented within six months, during which time there would be an extended ceasefire in the areas.

An evacuation of those wounded from both sides could begin on Friday.

Some 500 opposition prisoners would be freed from government jails under the deal.

Days before, Syrian government forces and Lebanese Shi’ite militant group Hezbollah on one side, and insurgents on the other, agreed a temporary ceasefire in the areas - the third since August.

Previous ceasefires in the border town, Zabadani, and the two villages, al-Foua and Kefraya, held only briefly.

Pro-government militia backed by Hezbollah have been defending al-Foua and Kefraya against insurgent attacks in mainly rebel-held Idlib province.

The Syrian army and Hezbollah are trying to capture Zabadani from besieged rebels.

One source said rebel fighters from Zabadani would be allowed to withdraw to Idlib province, taking only light weapons with them. In return, rebels in Idlib would let some 10,000 civilians from al-Foua and Kefraya leave the besieged villages.

Some 4,000 pro-government fighters in the villages would have to remain there.

During the six-month implementation, no fire was to be exchanged or air raids carried out, the sources said.

Implementation would be overseen by the U.N. office in Damascus, the sources said.

Insurgent group Ahrar al-Sham, which has been leading negotiations on the rebel side, could not be immediately reached for comment.