GENEVA -- The rival sides in Libya's conflict signed a permanent ceasefire Friday, a deal the United Nations billed as historic after years of fighting that has split the North African country in two. But skepticism over whether the agreement would hold began emerging almost immediately.

The breakthrough, which among other things orders foreign mercenaries out of the country, sets the stage for political talks in November to find a lasting solution to the chaos unleashed after a 2011 NATO-backed uprising toppled and killed longtime dictator Moammar Gadhafi.

Previous diplomatic initiatives to end the conflict have repeatedly collapsed -- but the UN-brokered deal aims to cement a monthslong lull in fighting and gives a boost to the political process.

"I am honoured to be among you today to witness a moment that will go down in history," Stephanie Turco Williams, the top UN envoy for Libya who led mediation talks this week, said at the signing in Geneva. She did, however, express some caution, noting that a "long and difficult" road remains ahead.

It's not clear how the ceasefire will be enforced -- given the patchwork of militias in Libya -- but Williams said armed groups and military units had agreed to return "to their camps" and that the deal would take effect immediately.

Foreign mercenaries will depart "from all Libyan territories land, air and sea" within three months, she added, referring to the thousands of Syrian fighters deployed by Turkey and Russia on opposite sides of the war. The agreement also involves the formation of a joint military force and a way to monitor violations, Williams said. The deal will be sent to the UN Security Council.

UN Secretary-General Antonio Guterres hailed the ceasefire as a "critical step," but said at a press conference at UN headquarters in New York that "there is much hard work ahead."

He urged regional actors to respect the ceasefire provisions and the international community to support Libyans in implementing them, including by upholding the widely flouted UN arms embargo.

Shortly after the announcement of the deal, Turkish President Recep Tayyip Erdogan said it did not appear to be credible -- even as he expressed hope that all parties would stick to it. "Time will show how long it will last," he added, questioning list of delegates and whether it would be "correct" for mercenaries to leave so quickly.

Following Libya's descent into chaos, a UN-recognized government holds sway in the capital, Tripoli, in western Libya, while the forces of military commander Khalifa Hifter run most of the east and south. Turkey is the main patron of the Tripoli government, while the United Arab Emirates, Russia and Egypt back Hifter. Both sides are also supported by an array of fractious militias, though the administrations often struggle to control them.

Ali Abushahma, a field commander for the administration in Tripoli and the head of its delegation, said: "We have had enough suffering, enough bloodshed."

"I appeal to all Libya: Be one hand," he added, warning of polarization by factions.

The agreement was welcomed by Egypt's foreign minister as well as German Foreign Minister Heiko Maas, whose country hosted a major international conference on Libya this year that sought to curb foreign powers' interference in the country.

The U.S. Embassy in Libya called it "a major step forward toward realizing the shared interests of all Libyans."

The meetings this week mark the fourth round of talks involving the Joint Military Commission under Williams' watch. Next month, the sides will go to Tunisia for political talks, with the goal of eventually holding elections.

Despite some skepticism, analysts say there is an opening now for a deal to bring an end to Libya's long-running war, as Prime Minister Fayez Sarraj of the Tripoli government plans to step down next month and the UN works to form a new, shared system of governance that spans the divided country.

Friday's deal follows a series of breakthroughs since fighting came to a halt in June, when Hifter's 14-month campaign to capture Tripoli collapsed.

International pressure has also been building on the parties to avert a battle over the strategic city of Sirte, the gateway to Libya's major oil export terminals. This fall, Hifter and allied eastern tribes announced the end of a monthslong blockade of Libya's oil facilities, initially imposed to protest alleged corruption in the oil sector and unfair distribution of revenues to the east.

Then last month, the two sides reached preliminary agreements to exchange prisoners and open up air and land transit across the country's divided territory.

On Friday, Williams said that a passenger plane flew from Tripoli to Benghazi for the first time in months, paving the way for a regular flights between eastern and western Libya. In another sign of progress, she said the key oil ports of Ras Lanuf and al-Sidr would soon resume production. Libya's prized light crude has long been a key factor in the civil war, with rival militias and foreign powers jostling for control of Africa's largest oil reserves.

To maintain the ceasefire, the two sides agreed to establish a "joint police operations room" that will help secure areas following the withdrawal of armed groups. The rivals also agreed to form a "joint limited military force" of personnel who report to the UN's Joint Military Commission.

Still, after years of violence, the talks are taking place in an environment of deep mistrust.

"Even as there has been a formal signing of a ceasefire, that does not at all mean that the armed groups on the ground will respect the terms of deal," said Mohamed Eljarh, co-founder of Libya Outlook, a consultancy based in eastern Libya. "As always with Libya, the devil is in the details, and the implementation."

------

DeBre reported from Dubai, United Arab Emirates. Associated Press writers Zeynep Bilginsoy in Istanbul, Geir Moulson in Berlin and Edith Lederer at the United Nations contributed to this report