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 Dec. 1.
Armenia's defense ministry said military action had halted and calm had been restored in the breakaway territory, which is internationally recognized as part of Azerbaijan but populated and, until recently, fully controlled by ethnic Armenians.
Russian President Vladimir Putin said the deal should pave the way for a lasting political settlement of a conflict that has killed thousands, displaced many more, and had threatened to plunge the wider region into war.
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. "This (ceasefire) statement has historic significance. This statement constitutes Armenia’s capitulation. This statement puts an end to the years-long occupation," Azeri President Ilham Aliyev said.
Some Azeris regretted fighting had ended before Azerbaijan controlled all of Nagorno-Karabakh.
Unrest broke out in Yerevan, the capital of Armenia, where crowds stormed and ransacked government buildings overnight, labeling the deal a betrayal. Some demanded the resignation of Prime Minister Nikol Pashinyan.
Nagorno-Karabakh leader Arayik Harutyunyan said there had been no option but to conclude a peace deal because of the risk of losing the whole enclave to Azerbaijan. Pashinyan said he had concluded the peace deal under pressure from his own army.
"The decision was made based on a deep analysis of the combat situation and in conjunction with the best experts," Pashinyan said.
Appealing to Armenians to see the deal as the start of a new era of national unity, he said, "This is not a victory, but there is no defeat until you consider yourself defeated."
In fighting that flared on Sept. 27, Azerbaijan says it retook much of the land in and around Nagorno-Karabakh that it lost in a 1991-94 war in which about 30,000 people were killed.
The capture of Shusha, or Shushi, appears to have been a turning point. Perched on a mountain top above Stepanakert, Nagorno-Karabakh's biggest city, it gave Azerbaijan's forces a commanding position from which to launch an assault.
Russia, which has a defense pact with Armenia and a military base there, is likely to hail the deal as a sign it is still the main arbiter in the energy-producing South Caucasus, which it sees as its own backyard, despite Turkish attempts to muscle in.
Russian peacekeepers will remain for at least five years, expanding Moscow's military footprint in the region. Putin said they would be deployed along the frontline in Nagorno-Karabakh and in a corridor between the region and Armenia.
The last of 10 military planes carrying Russian peacekeepers took off on Tuesday, the Russian defense ministry said. Almost 2,000 servicemen, 90 armored personnel carriers, and 380 vehicles and pieces of other hardware are being deployed.
Kremlin spokesman Dmitry Peskov said there had been no agreement on deploying any Turkish peacekeepers in Nagorno-Karabakh, but the Turkish military will help staff a joint monitoring center with Russian forces.
Under the agreement, Azerbaijan will gain a road link to an Azeri exclave on the Iranian-Turkish border, something that will give Turkey a land bridge to the rump Azerbaijan.
Putin said displaced people would be able to return to Nagorno-Karabakh and prisoners of war and bodies of those killed would be exchanged. All economic and transport links in the area would be reopened with the help of Russian border guards.
The agreement created the conditions for a long-term settlement "on a fair basis and in the interests of the Armenian and Azeri peoples," Putin said.
In Armenia, 17 political parties demanded Pashinyan resign and a petition was started demanding the agreement be annulled.
The secretary of Nagorno-Karabakh's security council, a top security body, quit in protest at the deal, Russia's RIA news agency reported.