TEHRAN, March 11 (AFP) - Regional powerhouses Iran and Saudi Arabia agreed Friday to restore ties and reopen diplomatic missions in a surprise, Chinese-brokered announcement that could have wide-ranging implications across the Middle East.

In a trilateral statement, Shiite-majority Iran and mainly Sunni Muslim Saudi Arabia said they would reopen embassies and missions within two months and implement security and economic cooperation deals signed more than 20 years ago.

Riyadh cut ties after Iranian protesters attacked Saudi diplomatic missions in 2016 following the Saudi execution of revered Shiite cleric Nimr al-Nimr -- just one in a series of flashpoints between the two longstanding rivals.

Friday's announcement, which follows five days of previously unannounced talks in Beijing and several rounds of dialogue in Iraq and Oman, caps a broader realignment and efforts to ease tensions in the region.

"Following talks, the Islamic Republic of Iran and the Kingdom of Saudi Arabia have agreed to resume diplomatic relations and reopen embassies and missions within two months," said the joint statement, which was published by both countries' official media.

The detente between Saudi Arabia, the world's biggest oil exporter, and Iran, a pariah for Western governments over its nuclear activities, has the potential to reshape relations across a region characterised by turbulence for decades.

Iran and Saudi Arabia support rival sides in several conflict zones including Yemen, where the Huthi rebels are backed by Tehran and Riyadh leads a military coalition supporting the government. The two sides also vie for influence in Syria, Lebanon and Iraq.