Russian peacekeeping forces are being deployed to the mountain enclave of Nagorno-Karabakh as part of a ceasefire deal designed to end six weeks of heavy fighting between Azerbaijan and ethnic Armenian forces.

Under the deal, Azerbaijan will keep territorial gains made in the fighting, including the enclave's second city of Shusha, which Armenians call Shushi.

Ethnic Armenian forces must give up control of a slew of other territories between now and 1 December.

Nagorno-Karabakh is a mountainous and heavily forested patch of land that sits inside the territory of the former Soviet republic of Azerbaijan.

Under international law, it is recognised as part of Azerbaijan, but the ethnic Armenians who make up the vast majority of the population reject Azeri rule.

They have been running their own affairs, with support from Armenia, since Azerbaijan's troops were pushed out in a war in the 1990s.

New fighting that broke out on 27 September quickly became the deadliest since the 1990s. Previous ceasefires brokered by Russia and the United States failed to hold.

The conflict has risked dragging in the big regional powers, Russia and Turkey, and destabilising the South Caucasus region, an important corridor for pipelines carrying oil and gas.

Russia has a defence alliance with Armenia, while Turkey backs its ethnic Turkic kin in Azerbaijan.

Following big Azeri territorial gains, Armenia's Prime Minister Nikol Pashinyan said he agreed to the new ceasefire under pressure from his own army, which said all military action in Nagorno-Karabakh had stopped.

Tensions flared in late 1980s

Long-standing ethnic tensionsbetween Christian Armenians and their mainly Muslim neighbours flared in the region - then part of the Soviet republic of Azerbaijan - in the late 1980s.

As the Soviet Union began to break up, it became apparent that Nagorno-Karabakh would come under the direct rule of the Azeri government. The ethnic Armenians did not accept that.

Sectarian conflict erupted, escalating into war in 1991 between Azerbaijan's troops and ethnic Armenian forces, backed by Armenia.

About 30,000 people were killed and many more were displaced.

Authorities in Nagorno-Karabakh declared independence that year but it was not recognised internationally, leaving the ethnic Armenian administration there in a state of legal limbo and under blockade from Azerbaijan's government.

By 1994, when an internationally brokered ceasefire was agreed, ethnic Armenians controlled almost all of Nagorno-Karabakh, plus some surrounding Azeri districts that gave them a buffer zone and land bridge connecting their region to Armenia.

Azerbaijan vowed to take back control over the territory,using military force if necessary.

International efforts over the years to find a lasting peace settlement, involving France, the United States and Russia as mediators, failed to clinch a deal.

Under the new ceasefire deal, Russian peacekeepers will stay in place for at least five years, expanding Russia's military footprint in the region.

President Vladimir Putin said they would be deployed along the frontline in Nagorno-Karabakh and in a corridor between the region and Armenia.

Deal welcomed by celebrations and protests

There has understandably been a mixed reaction to the ceasefire deal.

The ceasefire triggered celebrations in Baku, the capital of Azerbaijan, where cars and buses sounded their horns in delight and people cheered and waved the Azeri national flag.

But unrest broke out in Yerevan, the capital of Armenia, where crowds stormed and ransacked government buildings overnight.

The protesters labelled the deal a betrayal, with some demanded the resignation of Mr Pashinyan.