RIYADH, Saudi Arabia — Only five years ago, Crown Prince Mohammed bin Salman, the de facto ruler of Saudi Arabia, derided Iran’s supreme leader, saying he “makes Hitler look good.” Last week in a development that had the world doing metaphorical double takes, the Saudis not only re-established diplomatic relations with Tehran but also spoke gauzily of the countries “sharing one fate.”

The diplomatic rapprochement between Saudi Arabia and Iran after years of facing off in proxy conflicts across the Middle East was a coup for China, which facilitated the agreement. And it was a relief for Iran, which is grappling with domestic unrest and an economy waylaid by harsh sanctions.

But Saudi Arabia, too, has much to gain if the new cooperation truly takes root. The pact could help quiet the regional tensions that have inflamed wars, fueled media spats and sent missiles and drones flying across the Arabian Peninsula.

Resolving conflicts that have drained the Saudi government’s budget, stained its reputation and deterred potential investors has become a top priority for Prince Mohammed, as he overhauls the conservative Islamic kingdom’s economy and society, hoping to make it into a global hub for business and culture.

“The countries of the region share one fate,” the Saudi foreign minister, Prince Faisal bin Farhan, said on Twitter after the announcement. “That makes it necessary for us to work together to build models for prosperity and stability.”

The rivalry between the two Islamic nations, separated by less than 150 miles of Persian Gulf waters, has long shaped politics and trade in the Middle East.

It has a sectarian dimension — Saudi Arabia’s royal family and a majority of its populace are Sunni, while Iran’s people are overwhelmingly Shiite — but has mainly played out in proxy conflicts in Yemen, Iraq, Syria and Lebanon, where Iran has supported militias that Saudi officials say have destabilized the region.

The timing of the reconciliation was a surprise to many analysts; until recently, Saudi officials had said they were making little progress in talks with Iran. So, too, was the role that China played, hosting the discussions that led to the breakthrough.

Several Washington-based policy experts framed the Chinese involvement as a challenge to waning American dominance in the Middle East. Indeed, some Gulf Arab officials say that they can no longer rely on the United States to guarantee their security, that they must solve their own problems and that China is ready to offer weapons, technology and investment with no strings attached.

But other analysts cautioned that Prince Mohammed is simply leaning into the more pragmatic approach in foreign policy that he has followed over the past few years. While the United States remains the kingdom’s dominant security partner, they say Washington could not have brokered a deal between Saudi Arabia and Iran anyway, given its deeply strained relations with the Islamic Republic.

“It’s not like Saudi Arabia isn’t fully conscious that even a Chinese guarantee has its limits,” said Yasmine Farouk, a nonresident scholar at the Carnegie Endowment for International Peace, a Washington research group. “The Saudis have learned, over the past few years, very hard lessons, one of those being we have to continue in the diversification of our relationships.”

Saudi Arabia has long made clear that it was seeking a resolution with Iran. Saudi officials held several rounds of talks with their Iranian counterparts over the past two years, including in Iraq and Oman.

In an interview in 2019, Prince Mohammed said that a war between Saudi Arabia and Iran would send oil prices skyrocketing and spark the “total collapse of the global economy,” meaning that a “political and peaceful solution is much better than the military one.”

Just weeks before those remarks, a missile and drone assault on a major Saudi oil installation had briefly disrupted half of the kingdom’s crude production, an attack that U.S. officials said was directly overseen by Iran.

The realization that Iran had the audacity and ability to carry out such an operation — and that the attack had few immediate repercussions for the Islamic Republic — was a critical moment for Saudi officials, analysts have said. It appears to be part of what pushed them to enter into talks with Iran in 2021.

Since then, American efforts to revive the 2015 nuclear deal with Iran that was abrogated by President Donald J. Trump have faltered and stalled. Experts warn that Tehran now has enough enriched uranium to build several nuclear weapons if it chooses to, and Saudi officials fret that the kingdom could be their first target.

At the same time, Russia’s invasion of Ukraine has drawn the focus of global powers elsewhere, leaving Gulf governments with an even stronger sense that they must depend on themselves.

“Saudi foreign policy is very clear: They want to solve any differences, disagreements or disputes through diplomacy and have been trying very hard with the Iranians,” said Mohammed Alsulami, who heads an Iran-focused think tank in the Saudi capital of Riyadh.

“Saudi wants to try now,” he added, because “we are thinking that there might be some difficulties in the near future in terms of Iran’s relationship with the international community — the Russia crisis, enrichment. So let’s send this positive message and see.” Iran has implicitly taken Russia’s side in the Ukraine war by selling it drones that are attacking Ukrainian cities and infrastructure.

The move toward reconciliation is partly driven by the challenges Prince Mohammed faces at home as he overhauls nearly every aspect of life in Saudi Arabia. His all-encompassing transformation plan, “Vision 2030,” calls for diversifying the oil-dependent economy by attracting foreign investment and necessitates building new sectors like luxury tourism and entertainment.

He has said that he wants tens of millions of expatriates to move to the kingdom, and the government is bidding to host the World Expo in 2030. All of those targets would be particularly difficult to reach with an active war with Iran-backed rebels in Yemen on the kingdom’s southern border and missiles flying overhead.

Yet the shift toward resolving regional conflicts is also driven by the crown prince’s desire to transform Saudi Arabia into a global power in its own right, instead of the American “client state” that critical members of Congress have dismissed it as. Prince Mohammed sees the kingdom as the heavyweight and political leader of the Middle East, no longer as beholden to the United States as it once was, and he is eager to strengthen its relationships in Asia, Europe and Latin America.

He is also keen to portray the kingdom as a neutral arbiter in a world polarized by Russia’s invasion of Ukraine. The Saudi foreign minister traveled to Ukraine and Russia over the past month, disbursing humanitarian aid and offering to mediate the conflict.

That does not mean that Saudi Arabia is casting off the United States as its main security guarantor anytime soon; the United States supplies the vast majority of the kingdom’s weapons and defensive systems. But it does mean a relative weakening of America’s supremacy in the region, as Saudi officials expand ties in China, South Korea, India and beyond.

The first and most critical test of the new agreement with Iran will be played out in Yemen, where a Saudi-led coalition has been fighting the Iran-backed Houthi rebels since 2015. Saudi officials are eager to reach a deal to end the conflict, which has cost the Saudi government billions of dollars and sparked severe criticism of the kingdom in Washington and Europe. It has also killed hundreds of thousands of Yemenis and driven the country, the poorest in the Arab world, into a dire humanitarian crisis.

“In Yemen, we have a situation that is fragile but continuing on a positive trend,” the United Nations spokesman, Stéphane Dujarric, said at a news conference after the pact was announced. “We hope that this agreement will have a positive impact on this situation and others.”

Farnaz Fassihi contributed reporting from New York.