Saudi Arabia and Iran have agreed to resume diplomatic ties and reopen embassies, they said in a joint statement carried by the Saudi state news agency SPA.
The agreement was reached after talks in Beijing between top security officials from the two countries.
Tehran and Riyadh agreed "to resume diplomatic relations between them and re-open their embassies and missions within a period not exceeding two months", according to a statement issued by Iran, Saudi Arabia and China.
"The agreement includes their affirmation of the respect for the sovereignty of states and the non-interference in internal affairs."
They also agreed to activate a security cooperation agreement signed in 2001, as well as another earlier accord on trade, economy and investment.
Both countries thanked China, as well as Iraq and Oman for hosting earlier talks in 2021 and 2022.
The agreement was signed by Iran's top security official, Ali Shamkhani, and Saudi Arabia's national security adviser Musaed bin Mohammed Al-Aiban.
The US military said on Monday it destroyed six Iranian small boats and intercepted Iranian cruise missiles and drones as Tehran sought to thwart a new US naval effort to open shipping through the Strait of Hormuz.
A blast at a fireworks factory in China has killed at least 26 people and injured 61, flattening buildings and sending towering clouds of smoke into the sky, and prompting President Xi Jinping to order a thorough investigation, state media reported on Tuesday.
A Russian overnight missile and drone attack on Ukrainian gas production facilities killed five people, including two rescue workers, Ukrainian officials said on Tuesday, as Kyiv and Moscow exchanged rival ceasefire proposals.
Two cases of the deadly hantavirus have been confirmed, and five more are suspected among people who were on a luxury cruise ship now held in the Atlantic near Cape Verde, the World Health Organisation said in its most detailed update on the outbreak.