An Iranian protester holds a street sign bearing the name of prominent Shiite Muslim cleric Nimr al-Nimr during a demonstration against his execution by Saudi authorities, on January 3, 2016, outside the Saudi embassy in Tehran.

Editor’s Note: A version of this story first appeared in CNN’s Meanwhile in the Middle East newsletter, a three-times-a-week look inside the region’s biggest stories. Sign up here.

Abu Dhabi, UAE CNN  — 

Saudi Arabia and Iran announced on Friday that they had agreed to reestablish diplomatic ties after seven years of hostility, in a deal between the regional archrivals that could have wide-ranging implications for the Middle East.

Riyadh and Tehran plan to reopen their embassies within two months in an agreement mediated by China, Saudi Arabia and Iran said in a joint statement after talks in Beijing on Friday.

They also plan to reimplement a security pact signed 22 years ago under which both parties agreed to cooperate on terrorism, drug-smuggling and money-laundering, as well as reviving a trade and technology deal from 1998.

Friday’s announcement is also a diplomatic victory for China in a Gulf region that has long been considered part of the US’ domain of influence. It comes as the Biden administration tries to notch its own win in the Middle East by trying to broker a normalization pact between Israel and Saudi Arabia.

Talks had been ongoing since March 6 in Beijing between Iranian national security chief Ali Shamkhani, Saudi national security council adviser Mosaed Bin Mohammad Al-Aiban and China’s top diplomat Wang Yi, according to Iranian state media.

Video of the signing ceremony aired by Iranian media showed officials seated around tables on opposite sides with the Saudi Arabian, Iranian and Chinese flags around them.

“We will continue to play a constructive role in properly handling hotspot issues in today’s world in accordance with the wishes of all countries and demonstrate our responsibility as a major country,” Wang said, adding that Chinese President Xi Jinping supported it since the beginning.

In an apparent push back to American influence, Wang said that “the world is not limited to the Ukraine issue” while emphasizing that the fate of the Middle East should be determined by the people of the Middle East.

“The foreign ministers of the two countries will meet each other to implement this decision and make necessary arrangements for the exchange of ambassadors,” the joint statement said. “The two sides agree to respect the sovereignty and non-interference in each other’s internal affairs.”

Saudi Arabia and Iran had previously held talks aimed at reconciliation in Oman and Iraq.

Riyadh severed ties with Tehran in 2016 after Iranian protesters stormed the Saudi embassy in the Iranian capital following the execution of a Shi’ite cleric in Saudi Arabia. Since then, they have fought a proxy war that has embroiled a number of neighboring countries, bringing the region ever closer to war.

In Yemen, the two countries have supported opposite sides of a civil war that has been described by the United Nations as one the world’s worst humanitarian crises. From there, the Houthi rebels have fired missiles at both Saudi Arabia and the United Arab Emirates, targeting oil infrastructure that is vital to their economies.

Saudi Arabia has however been engaged in direct talks with the Houthis, and an unofficial ceasefire appears to be holding.

In a speech prior to the signing of the agreement with his Iranian counterpart, Saudi Arabia’s Al-Aiban called for non-interference in the sovereignty of states to be a “fundamental pillar for the development of relations.”

“We value what we have reached and we hope that we will continue the constructive dialogue, in accordance with the pillars and foundations included in the agreement,” he said.

Iranian Foreign Minister Hossein Amir-Abdollahian hailed the deal and said Tehran would step up its diplomatic efforts in the region.

“Iran and Saudi Arabia relations returning to normal makes the two countries, the region and the Muslim world stronger,” Amir-Abdollahian tweeted on Friday.

Tehran finds itself increasingly isolated on the global stage. Talks to revive Iran’s 2015 nuclear pact with world powers are frozen and relations with Western states have faced further strains due to the Islamic Republic’s brutal crackdown on protests that started in September.

Iran’s main international ally Russia is preoccupied with the war in Ukraine, while China, its other ally, has lately been courting Tehran’s archrival Saudi Arabia.

“For Saudi Arabia, rapprochement with Iran is a key part of a major diplomatic offensive on all fronts,” said Hussein Ibish, a senior resident scholar at the Arab Gulf States Institute in Washington. “Just as Saudi Arabia normalizes relations with Iran it produces a potential framework for eventually, perhaps, normalizing with Israel as well.”

“The fact that it has been agreed to in Beijing is very significant for China and its rise as a diplomatic and strategic player in the Gulf region,” said Ibish. “This does seem to recognize China’s unique role in being able to broker relations between Tehran and Riyadh, stepping into a position that had previously been occupied by European countries, if not the United States, and this will not be particularly pleasing to Washington.”

The reconciliation comes as China expands its diplomatic outreach in the Arab world. In December, Chinese President Xi Jinping was welcomed in Riyadh in an extravagant ceremony as part of a visit that brought together 14 Arab heads of state. That was just months after a relatively low-key meeting with US President Joe Biden, whose relationship with Saudi Arabia has been frosty.

“China’s role as a broker is striking, and could foreshadow a bolder diplomatic position,” said Sanam Vakil, a senior research fellow for the Middle East and North Africa program at Chatham House think-tank in London.

“But we should be cautious in overstating Beijing’s intentions. This is more about China’s interests in the region. China has brought together two key actors – regional and economic actors – for the purpose of reducing regional tensions and facilitating greater economic engagement with both.”

Xiaofei Xu, Shawn Deng and Abbas Al Lawati contributed to this report.