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.