Armenia, Azerbaijan and Russia have signed an agreement to end military conflict over the disputed enclave of Nagorno-Karabakh.
Armenian Prime Minister Nikol Pashinyan called the deal "incredibly painful both for me and both for our people".
It follows six weeks of fighting between Azerbaijan and ethnic Armenians.
The region is internationally recognised as Azerbaijani but has been run by ethnic Armenians since 1994.
A Russian-brokered truce was signed at the end of the war in the early 1990s but there was no peace deal.
Although both sides took steps to reduce tensions last year, fighting erupted at the end of September and several attempts to end the conflict failed.
The new ceasefire agreement prompted anger in Armenia, as protesters stormed the parliament, beating up the speaker and reportedly looting the prime minister's office.
What has been agreed?
The peace deal, which was signed by Russian President Vladimir Putin, Azerbaijani President Ilham Aliyev and Armenia's prime minister, took effect on Tuesday from 01:00 local time (21:00 GMT Monday).
Under the deal, Azerbaijan will hold on to areas of Nagorno-Karabakh that it has taken during the conflict. Armenia has also agreed to withdraw from several other adjacent areas over the next few weeks.
The BBC's Orla Guerin in Baku says that, overall, the deal should be read as a victory for Azerbaijan and a defeat for Armenia.
During a televised online address, President Putin said that Russian peacekeepers would be deployed to patrol the front line.
Russia's defence ministry confirmed that 1,960 personnel would be involved and reports said planes had left an airbase at Ulyanovsk on Tuesday carrying peacekeepers and armoured personnel carriers to Karabakh. Part of their role will be to guard the "Lachin corridor", which links the Karabakh capital, Stepanakert, to Armenia.
Turkey will also take part in the peacekeeping process, according to Azerbaijan's president, who joined President Putin during the address.
President Putin said the agreement would include an exchange of war prisoners, with "all economical and transport contacts to be unblocked."
Jubilant scenes in Azerbaijan
By Orla Guerin, BBC News, Baku
There is a mood of national celebration in Baku. At Martyrs' Alley - a memorial to Azerbaijan's fallen soldiers - there are surging crowds among a sea of flags.
There is a real sense here that a key victory has been achieved after a very long fight. Groups are breaking out in chants and singing the national anthem.
Two young students are holding a homemade sign that reads: "Tell the world we are coming home." It is a home they have never seen. Their parents were driven from Nagorno-Karabakh almost 30 years ago, but now they intend to move there to build a new life.
Asked if they could imagine living side by side with Armenians, one replied that this might take decades. "Our children may see this," she told me.
How have leaders reacted?
President Aliyev said the agreement was of "historic importance," and amounted to a "capitulation" by Armenia.
Armenia's prime minister said that his decision had been based on "deep analyses of the combat situation and in discussion with best experts of the field".
"This is not a victory but there is not defeat until you consider yourself defeated," Mr Pashinyan said.
The Armenian leader in Nagorno-Karabakh, Arayik Harutyunyan, said a ceasefire had been unavoidable after the loss of Karabakh's second biggest town, Shusha (known as Shushi in Armenian).
Battles were already taking place on the outskirts of Karabakh's main city, Stepanakert, and if the conflict had continued the whole of Karabakh would have been lost, he said on Facebook. "We would have far more losses," he said.
In the Armenian capital Yerevan, a large crowd gathered to protest against the agreement, according to local media. They broke into parliament and government buildings, shouting "We will not give it up."
Protesters ransacked the prime minister's official residence and Mr Pashinyan said they "stole a computer, a clock, perfume, drivers licence and other items".
Defeat sparks crisis in Armenia
By Yuri Vendik, BBC Russian, Yerevan
It was a night of unrest and upset in Armenia's capital, where crowds of protesters stormed government buildings and the country's parliament.
But by Tuesday morning, that anger appeared to have died down.
Leading politicians, with the exception of Prime Minister Nikol Pashinyan, are in talks to find a way out of this deep crisis. And this is a crisis: the defeat in Nagorno-Karabakh is a devastating one
The protesters who gathered here overnight accused the government of betrayal. They believed the fighting should have continued until the end and they were confident of victory.
But in Nagorno-Karabakh itself there was no such optimism. The leadership of the enclave earlier admitted that, had the fighting continued, the main city of Stepanakert would have been lost within days.
What's happened during the conflict?
The Armenians steadily lost territory and significantly over the weekend Azerbaijani forces took over Shusha.
Azerbaijan also admitted to mistakenly shooting down a Russian military helicopter over Armenia, killing two crew members and injuring a third.
It is unclear exactly how many have died. Both sides deny targeting civilians but accuse the other of doing so.
Nagorno-Karabakh's authorities say nearly 1,200 of its defence forces have died in the fighting, and civilians have also been killed or injured.
Azerbaijan has not released its military casualty figures but has said more than 80 civilians were killed in the fighting - including 21 in a missile strike on the town of Barda last month.
Russian President Vladimir Putin said last month that almost 5,000 people had been killed in the fighting.
What ties does Russia have to both sides?
Russia has a military base in Armenia, and the two countries are members of the Moscow-led Collective Security Treaty Organization.
The treaty envisages Russia's military support if Armenia is attacked - but it does not include Nagorno-Karabakh or the other Azerbaijani regions around it seized by Armenian forces.
At the same time, Moscow also has strong ties to Azerbaijan, which is being openly backed by Turkey, a Nato member. Russia has been selling weapons to both Armenia and Azerbaijan.