BEIJING — India and China have withdrawn troops from a disputed Himalayan region on their border, foreign ministries from the two countries announced Monday, defusing a tense standoff that had threatened to provoke armed conflict between the nuclear-armed Asian rivals.

For the past two months, Indian and Chinese troops had faced off on a plateau in the Doklam area in the Himalayas after Indian troops moved in to prevent the Chinese military from building a road into territory claimed by India's close ally, Bhutan.

China had denounced the Indian move as a direct infringement of its sovereignty, demanded an immediate and unconditional withdrawal, and warned that conflict was a real possibility if that didn't happen.

On Monday, the two sides announced they had reached an agreement, with India saying its troops were disengaging and China saying it would redeploy forces in response. By the evening, India said both sides had almost completed their withdrawals.

China said it would continue to patrol and garrison the area, and to exercise its sovereign rights.

.

Neither side was willing to admit to having backed down.

"We remind India to learn the lessons from this incident, tangibly abide by the historical treaties and the basic principles of international law, and to meet China halfway, jointly guard the peace and tranquility of the border areas, and promote a healthy development of bilateral military relations," Chinese Defense Ministry spokesman Wu Qian said in a statement.

India said it had insisted on resolving the dispute through diplomatic channels. "Our principled position is that agreements and understandings reached on boundary issues must be scrupulously respected," the Ministry of External Affairs said.

An Indian foreign ministry official also told the Associated Press that the two sides had agreed to return to the "status quo." The cable news channel NDTV reported that Chinese bulldozers had moved away and road construction stopped, according to its sources — implying that India's demand had been met.

Beijing also wanted to resolve the dispute ahead of a meeting scheduled to take place in China this weekend of heads of state from the "BRICS" countries — Brazil, Russia, India, China and South Africa.

"It's hugely good news," said Wang Dehua, an Indian studies expert at the Shanghai Institute for International Studies. 

He said China would try to address India's security concerns when Prime Minister Narendra Modi visits for the summit but would continue building roads in border areas.

Mao Siwei, former Chinese consul general in the Indian city of Kolkata, said the statements were deliberately "vague" because of the sensitivity of the issue and the reluctance of either side to show weakness.

"Judging from experience and common sense, I guess both sides have come to the following agreement: Firstly, on principle, China would stop its road building and India would withdraw its troops; secondly, regarding the timing, India would withdraw first and China would withdraw later."

The tiny Himalayan kingdom of Bhutan was inadvertently swept up into the dispute when Indian soldiers moved from a nearby garrison into territory Bhutan contests with China to block a road-building crew from China's People's Liberation Army.

Bhutan's government was careful not to make comments and inflame tensions; aside from one brief statement from its Foreign Ministry, it maintained a calculated silence throughout the dispute.

Gowen reported from New Delhi. Shirley Feng and Luna Lin in Beijing contributed to this report.