Iran and Saudi Arabia have agreed to re-establish diplomatic relations and reopen their respective embassies after years of hostility, in a diplomatic breakthrough brokered by China.

The agreement, announced Friday after five days of high-level talks in Beijing, is likely to help ease tensions in the Middle East and signals weakening U.S. influence in the region.

Iran's top security official Ali Shamkhani (R), China's top diplomat Wang Yi (C) and Musaad bin Mohammed al-Aiban, Saudi Arabia's national security adviser, pose for a photo in Beijing on March 10, 2023, after Iran and Saudi Arabia have agreed to resume bilateral diplomatic ties. (Chinese Foreign Ministry/Handout/Anadolu Agency/Getty/Kyodo)

The two Gulf states will reopen their embassies "within a period not exceeding two months" and agreed that their foreign ministers "shall meet to implement" the deal, according to a trilateral statement.

Iran and Saudi Arabia expressed their appreciation to Iraq and Oman for hosting rounds of dialogue from 2021 to 2022, and to China for hosting the latest talks and contributing to the success of the negotiations, the statement said.

The statement was issued by Ali Shamkhani, secretary of Iran's Supreme National Security Council, Musaad bin Mohammed al-Aiban, national security advisor of Saudi Arabia, and Wang Yi, director of the Office of the Foreign Affairs Commission of the Communist Party of China Central Committee.

Saudi Arabia cut ties with Iran in 2016 after demonstrators attacked the Saudi Embassy in Tehran, following Riyadh's execution of a prominent Shiite cleric.

Congratulating Tehran and Riyadh for taking a major step forward, Wang said Friday that the improved ties have opened a path leading to peace and stability in the Middle East, according to China's official Xinhua News Agency.

"As a reliable friend of the two countries, China will continue to play a constructive role," said Wang, Beijing's top foreign policy official, was quoted by Xinhua as saying.

White House National Security Council spokesman John Kirby said Saudi Arabia had kept the United States informed about the talks in Beijing, but that Washington was not directly involved.

Speaking at a press briefing in Washington, Kirby dismissed the notion that an Iran-Saudi deal in Beijing suggests a rise of Chinese clout in the Middle East.

"I would stridently push back on this idea that we're stepping back in the Middle East -- far from it," he was quoted as saying by the Associated Press.

U.N. Secretary General Antonio Guterres welcomed Friday's deal and praised China, Iraq and Oman for promoting the negotiations.

"Good neighborly relations between Iran and Saudi Arabia are essential for the stability of the Gulf region," Stephane Dujarric, spokesman for Guterres, said in a statement.

Egypt's Foreign Ministry said the country is "following with interest" the agreement, and looks forward to it contributing to easing tensions in the region.

Japanese Prime Minister Fumio Kishida said his government is "closely monitoring the situation" over the Chinese-brokered deal.

"It is indispensable from the perspective of energy security to ensure peace and stability in the Middle East region," Kishida told reporters Saturday in Fukushima, northeastern Japan.

He pledged to continue diplomatic efforts to help ease tensions in the region and strengthen ties with Gulf countries.