The two countries released a joint communique with China on the deal, which apparently brokered the agreement.
The Saudi Press Agency SPA published the statement on Friday.
Chinese state media did not immediately report on the deal.
Iranian state media posted images and video it described as being taken in China with the meeting.
It showed Ali Shamkhani, the secretary of Iran’s Supreme National Security Council, with Dr. Musaed bin Muhammad Al-Aiban, Minister of State and National Security Adviser in Saudi Arabia with Wang Yi, the director of foreign affairs in the Central Committee of the Chinese Communist Party.
"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," the official Iranian news agency IRNA said, citing a joint statement.
China, which recently hosted Iran's President Ebrahim Raisi, is also a top purchaser of Saudi oil. President Xi Jinping, just awarded a third five-year term as president earlier on Friday, visited Riyadh in December to attend meetings with oil-rich Gulf Arab nations crucial to China’s energy supplies.
Iran's state-run IRNA news agency quoted Shamkhani as calling the talks "clear, transparent, comprehensive and constructive.”
“Removing misunderstandings and the future-oriented views in relations between Tehran and Riyadh will definitely lead to improving regional stability and security, as well as increasing cooperation among Persian Gulf nations and the world of Islam for managing current challenges,” Shamkhani was quoted as saying.
Dr. Musaed bin Muhammad Al-Aiban headed the Saudi delegation at the talks, according to the SPA.
The agreement includes a commitment to respect the sovereignty of countries and non-interference in their internal affairs, according to SPA.
The foreign ministers of both countries will meet to activate the agreement, arrange the exchange of ambassadors, and discuss ways to strengthen relations.
Additionally, the security cooperation agreement signed in 2001 and the general agreement for cooperation in various fields signed in 1998 will be activated.
The three countries expressed their commitment to enhancing regional and international peace and security, added SPA.
Riyadh cut ties with Tehran after Iranian protesters attacked Saudi diplomatic missions in the Islamic republic in 2016 following the Saudi execution of Shia cleric Nimr al-Nimr.
Ali Shamkhani 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", IRNA said.
Shia-majority Iran and Sunni Muslim Saudi Arabia support rival sides in several conflict zones across the Middle East, including in Yemen where the Houthi rebels are backed by Tehran, and Riyadh leads a military coalition supporting the government.
Iraq, a neighbour to both countries, had hosted several rounds of talks between Iran and Saudi Arabia since April 2021.
Those encounters were held at a relatively low level, involving security and intelligence officials.
In Friday's statement, Iran and Saudi Arabia said they "thank the Republic of Iraq and the Sultanate of Oman for hosting the talks held between the two sides in 2021 and 2022 as well as the leaders and government of the People's Republic of China for hosting and supporting the talks held in that country."
Iraq hails deal
Iraq has welcomed the agreement reached between Saudi Arabia and Iran, saying it would improve regional cooperation and fulfill the aspirations of all parties.
"A new page has been opened in diplomatic relations between the two countries," said a statement from the foreign ministry of Iraq that was published by the Iraqi News Agency (INA).
The statement noted that "the efforts made by the Iraqi government through Baghdad hosting the dialogue rounds between the two sides" paved the way for reconciliation.
It added that these Iraqi efforts "established a solid base for the dialogues that followed through the Sultanate of Oman and the People's Republic of China, leading to the moment of agreement."
The integration of relations between Saudi Arabia and Iran would improve regional cooperation and fulfill the aspirations of all parties, heralding the inauguration of a new phase, the statement stressed.
Israel and the US
Shortly after the news broke, former Israeli Prime Minister Naftali Bennett took to his official Twitter account on Friday to express his concerns regarding the renewal of relations between Saudi Arabia and Iran.
Bennett said: "The renewal of relations between Saudi Arabia and Iran is a serious and dangerous development for Israel and is a political victory for Iran. This is a fatal blow to the effort to build a regional coalition against Iran."
His remarks reflect concerns in Israel that Iran's regional influence constitutes a threat to Israel's security.
Meanwhile, a White House National Security Council spokesperson said the US is aware of reports that Iran and Saudi Arabia have resumed diplomatic ties, but referred further details to the Saudis.
"We welcome any efforts to help end the war in Yemen and de-escalate tensions in Mideast."
"De-escalation and diplomacy together with deterrence are key pillars of the policy President Biden outlined during his visit to the region last year," the White House spokesperson told Reuters.
Tensions between Israel and the Biden administration have come to the fore recently due to their differing approaches towards Iran's nuclear ambitions.
While the US is advocating for diplomatic means to prevent Iran from acquiring nuclear weapons, Israel has not ruled out the possibility of military action to achieve the same objective.
US Defense Secretary Lloyd Austin has recently visited Israel and discussed the issue with Israeli Defense Minister Yoav Gallant.
Austin reiterated the US's stance on diplomacy, while Gallant emphasized that "all measures necessary" must be taken to prevent Iran from acquiring nuclear weapons.
The Biden administration is seeking to revive the 2015 nuclear deal, which gave Iran relief from economic sanctions in exchange for curbs on its nuclear program.
However, talks have stalled for months, and the recent reconciliation between Saudi Arabia and Iran has added an additional layer of complexity to an already delicate situation.
The ongoing tensions between the US, Israel, and Iran have caused concerns globally, particularly given the potential for military conflict in the region.