Shi'a -majority Iran and predominately 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.
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.
Ali Shamkhani, the secretary of Iran's Supreme National Security Council, had travelled to Beijing on Monday for "intensive negotiations with his Saudi counterpart in China in order to finally resolve the problems between Tehran and Riyadh", Iran's official IRNA news agency said.
"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.
Iran's Foreign Minister Hossein Amir-Abdollahian welcomed the rapprochement and said Tehran will "actively prepare other regional initiatives".
"The return to normal relations between Tehran and Riyadh offers great opportunities to the two countries, the region and the Muslim world," he tweeted.
Amir-Abdollahian had said in July that the two countries were ready to move talks to a higher level, in the political and public spheres.
But no talks had been publicly announced since April last year.
The detente between Saudi Arabia, the world's biggest oil exporter, and Iran, a pariah for Western governments over its nuclear program, has the potential to reshape relations across a region characterized by turbulence for decades.
Riyadh cut ties after Iranian protesters attacked Saudi diplomatic missions in 2016 following the Saudi execution of revered Shi'a cleric Nimr al-Nimr - just one in a series of flashpoints between the two longstanding rivals.
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.
Iraq, the United Arab Emirates, Qatar and the Gulf Cooperation Council also hailed the announcement.
The White House also welcomed the deal, but said it remains to be seen whether the Iranians would "meet their obligations."
"It kind of sets the scene for the region's two superpowers to start to hash out their differences," said Dina Esfandiary of the International Crisis Group.
"The potential downside of that, of course, is that if they are the ones who are divvying up the region and sorting things out amongst themselves, you start to lose sight of regional contexts and grievances, which could potentially be problematic," she added.
The pledge to resume ties follows a broad pattern of attempts to settle regional disputes, including the Saudi-led blockade of Qatar, which lasted from June 2017 to January 2021.
Saudi Arabia and its allies the UAE, Bahrain and Egypt broke off ties claiming gas-rich Qatar supported extremists and was too close to Iran - allegations that Doha denied.
Other Gulf states had also scaled back their ties with Iran after the 2016 incident.
But in September, Iran welcomed an Emirati ambassador back after a six-year absence. A month earlier, Iran said Kuwait had sent its first ambassador to Tehran since 2016.