Eager to end its political and economic isolation, Iran had been trying for two years to restore ties with Saudi Arabia, the Arab heavyweight and oil powerhouse.
Last September, Iran's Supreme Leader Ali Khamenei lost patience with the slow pace of bilateral talks and summoned his team to discuss ways to accelerate the process, which led to China's involvement, two Iranian officials told Reuters.
Beijing's secret role in the breakthrough announced last week shook up dynamics in the Middle East, where the US was for decades the main mediator, flexing its security and diplomatic muscles.
"The Chinese showed willingness to help both Tehran and Riyadh to narrow the gaps and overcome unresolved issues during the talks in Oman and Iraq," said an Iranian diplomat involved in the talks.
The deal was struck after a seven-year diplomatic rupture that took place when the Iranian embassy in Tehran and consulate in Mashhad were attacked in January 2016.
Asked whether the Saudi-Iran deal might fray, Wang Di, a senior Chinese diplomat involved in the talks in Beijing, told reporters the rapprochement was a process without expectations that all issues would be solved overnight.
"The important thing is for both sides to have the sincerity to improve ties," he said, according to state Xinhua news agency reporter Yang Liu on Twitter.
Saudi Arabia began exploring ways to open a dialogue with Iran two years ago in Iraq and Oman, said a Saudi official.
This led to a critical moment in December, when Chinese President Xi Jinping visited Riyadh where he expressed his desire to broker dialogue between Saudi Arabia and Iran.
"Saudi Arabia welcomed this and promised to send, for us to send to the Chinese side, a summary of the previous rounds of dialogue, a plan on what we think on how we can resume these talks," said the Saudi official.
In February, Iran's President Ebrahim Raisi visited Beijing and the Chinese forwarded Riyadh's proposals that were accepted by the Iranian side, the official added.
Chinese mediation ‘best option’
An Iranian official said the deal covered a range of issues, from security concerns to economic and political issues.
"I will not go into details, but we have agreed that neither country will be a source of instability for the other one. Iran will use its influence in the region, particularly in Yemen, to help Riyadh's security," the official said.
"Both sides will do their best to preserve security in the Persian Gulf, guarantee the oil flow, work together to resolve regional issues, while Tehran and Riyadh will not get involved in military aggression against each other."
Iran chose its senior national security official Ali Shamkhani to lead the negotiations because he is an ethnic Arab, said a regional source who belongs to Khamenei's inner circle.
"China was the best option considering Iran's lack of trust towards Washington and Beijing's friendly ties with Saudi Arabia and Iran. China also will benefit from a calm Middle East considering its energy needs," said an Iranian official, briefed about the meetings.
After decades of mistrust, ongoing frictions should not come as a surprise. "This agreement does not mean that there will be no issues or conflicts between Tehran and Riyadh. It means that whatever happens in the future it will be in a 'controlled' way," said an Iranian insider, close to Iran's decision-making elite.