AMMAN/BEIRUT (Reuters) - Syrian jihadist fighters and rival Islamist insurgents agreed a ceasefire on Friday to end days of infighting, a statement said, after al Qaeda-linked militants surrounded the other rebels near a Syria-Turkey border crossing.

The fighting between Hayat Tahrir al-Sham, an alliance led by al Qaeda’s former Syria branch, and the more moderate Islamist Ahrar al-Sham plus mainstream rebel factions broke out in insurgent stronghold Idlib province this week.

Ahrar al-Sham said in an online statement that under the ceasefire, it and other rebel factions would “leave the Bab al-Hawa crossing and turn it over to civilian administration”.

The clashes have been the heaviest yet between the two sides who have long vied for influence in the only province in Syria that is entirely under rebel control.

Tahrir al-Sham had on Friday advanced in several areas, including towards Bab al-Hawa, an important supply route for Turkish-backed rebels fighting under the Free Syrian Army (FSA) banner.

Mainstream foreign-backed rebels fighting under the FSA banner have increasingly lost out to more powerful Islamist groups in Syria’s six-year conflict.

In blow to those rebels, the U.S. administration decided to halt a CIA program to equip and train some groups fighting against President Bashar al-Assad, U.S. officials said this week.

The agreement for Ahrar and its allies to withdraw from Bab al-Hawa is yet another setback.

A resident of Atmeh, a town north of the crossing, who is in touch with local officials and rebels said earlier Bab al-Hawa had effectively been encircled from the Syrian side.

Tahrir al-Sham “has taken control of the hills around Bab al-Hawa”, bringing them to within 1 km (mile) of the crossing, he said. “Just the crossing remains” under the control of Ahrar al-Sham, he said.

A source in Ahrar al-Sham also said Tahrir al-Sham had surrounded the crossing, but vowed not to give it up.

Tahrir al-Sham later released a statement urging Ahrar al-Sham fighters at Bab al-Hawa to surrender, saying they would not be harmed if they did.

“To our brothers in Ahrar al-Sham leadership, come out strong and dignified - we will give you security and peace and sit together to come up with a project that unifies us,” it said.

War monitors reported that Tahrir al-Sham attacked military positions of Ahrar al-Sham on Tuesday.

In clashes early this year, several more hardline Islamist groups joined Tahrir al-Sham, which is headed by al Qaeda’s former Syria branch, previously known as the Nusra Front.

Ahrar al-Sham sided with FSA groups fighting against the Tahrir al-Sham alliance.

Rebels say the infighting takes the focus away from the battle against Assad’s forces and weakens insurgents.