Rival militia factions that have been fighting in Libya's capital Tripoli for the past week have agreed a ceasefire, the United Nations says.

At least 47 people have been killed and 1,800 families internally displaced by the violence, officials say.

Last week, a ceasefire deal announced by officials from western cities only held for a few hours.

A UN-backed government is nominally in power in the capital, but militias occupy much of the rest of the country.

"Under the auspices of [UN envoy Ghassan Salame], a ceasefire agreement was reached and signed today to end all hostilities, protect civilians, safeguard public and private property," the Unsmil mission said.

The ceasefire also provides for the reopening of the capital's only functioning airport, Mitiga, which has been closed since 31 August due to the clashes.

The agreement "does not aim to fix all the Libyan capital's security problems; it seeks to agree on a broader framework on the way to start addressing these issues," the mission said.

The talks included military officers, leaders of various armed groups in and around the capital and the UN-backed Government of National Accord (GNA).

This is the third attempt to end the outbreak of violence in Tripoli as the first two ceasefires were broken almost immediately.

This time there's a signed agreement mediated by the United Nations.

The armed group that sparked the eight-day conflict posted a statement saying its commander supported the truce; the group claims the agreement includes the departure of its rivals from Tripoli.

Those rivals are militia brigades which are nominally attached to the interior ministry in the Tripoli government.

'Volatile' situation
The violence has forced thousands of people to flee to nearby towns or seek shelter in other parts of the capital, while many more have remained trapped inside their homes.

The violence broke out last week when militias from a city to the south of Tripoli attacked its southern areas, leading to fighting with local militias that support the GNA.

Hundreds of migrants trapped by the fighting have been moved to other detention centres.

Libya has faced continuing chaos since Nato-backed forces overthrew long-serving ruler Col Muammar Gaddafi in October 2011.