More recently, Saudi Arabia supported anti-Kremlin rebels in the restive republic of Chechnya. The kingdom maintains ties with Russia’s predominantly Muslim regions, including Chechnya, where the conflict was brutally ended by Mr. Putin and his local ally, Ramzan A. Kadyrov, leader of the Chechen Republic.

The symbolism of the Saudi king traveling to Moscow, just months after President Trump flew to Riyadh and lavished praise on his Saudi hosts, spoke volumes about changing relationships in the Middle East.

In recent years, Saudi Arabia and Russia were at odds in Syria, where the kingdom partnered with the United States and other countries to back rebels seeking to overthrow Mr. Assad. Russia, Mr. Assad’s longtime ally, sent forces in 2015 that turned the tide, and now Mr. Assad’s future appears secure.

As the Saudis have given up on the possibility of regime change in Damascus, their stance toward Russia has changed. Russia is likely to seek Saudi support for its efforts to establish “de-escalation zones” to bring down the violence, analysts said. And Saudi Arabia is likely to seek Russian help in pushing back against Iran, its regional nemesis.

But analysts doubted that Russia would take the Saudi side against Iran, given its battlefield partnership with Iran to help Mr. Assad in Syria.

“Definitely, Russia’s operation in Syria put Moscow into the front line of Middle Eastern politics,” said Grigory Kosach, expert for the Russian International Affairs Council. “Saudi Arabia believes that they now have to build relations with Russia because it plays a significant role in their home region. However, Russia is acting there in tandem with Iran, which worries the Saudis.”

Oil has brought them closer, too.

After the sharp drop in oil prices in 2014, both nations faced similar perils: budget deficits and the need to diversify their exports. Being the two biggest oil producers, they were the principal backers of an oil production freeze agreement that stopped the price fall.