While Europe and the United States were trying to isolate Moscow for its invasion of Ukraine, President Recep Tayyip Erdogan of Turkey referred to Russia’s president as “my friend.” While NATO leaders worked to enlarge the alliance, Mr. Erdogan held up the process by seeking concessions for his nation.

Then on Monday, Mr. Erdogan suddenly flipped after more than a year of being treated as an in-house spoiler by Western allies. He dropped his objections to Sweden joining the alliance and allowed a NATO summit to convene on Tuesday with a new sense of strength and unity.

Mr. Erdogan has yet to comment publicly on his decision, much less explain his change of heart, but he appears to have concluded that he had little more to gain from continuing to block Sweden — and that he could potentially benefit more from mending his sour relationships with the United States and other NATO allies.

Already on Tuesday, Mr. Erdogan cast a one-on-one meeting with President Biden in terms of a bright new start between the two leaders, who have had chilly relations. “All of our previous meetings were like warm-up rounds, but right now we launch a new process,” Mr. Erdogan told Mr. Biden when they met ahead of the summit in Vilnius, Lithuania.

As Mr. Erdogan has made gestures of rapprochement, he has also given recent indications that he is distancing Turkey from Russia, a shift that would ingratiate him with Western leaders and further isolate President Vladimir V. Putin of Russia.

“This is not Erdogan’s first U-turn and will not be the last,” said Osman Sert, the research director for PanoramaTR, a Turkish risk analysis organization. “Mr. Erdogan knows he needs to do something to create a bridge to the West.”

Since Russia invaded Ukraine last year, Turkey has occupied a unique middle ground between Moscow and the West. Mr. Erdogan condemned the invasion and closed the Turkish straits to most military ships, limiting Russia’s ability to bolster its fleet in the Black Sea.

At the same time, Turkey not only refused to impose sanctions on Russia but has expanded trade ties, stepping up Turkish exports to Russia and buying cut-rate Russian gas.

Turkish officials argue that Turkey’s position has made it a necessary diplomatic intermediary, helping to broker prisoner swaps and a U.N. deal to ensure the export of Ukrainian grain through the Black Sea. Still, Mr. Erdogan’s frequent and apparently friendly meetings with Mr. Putin left Western officials wondering where his true allegiances lay.

But on the sidelines of the NATO summit Lithuania on Tuesday, the mood was more jovial as Mr. Erdogan and Mr. Biden spoke to reporters before their talks.

Mr. Biden welcomed Mr. Erdogan to what he called a “historic meeting” and Mr. Erdogan described it as “the first step” in a stronger relationship.

He said he wished to work with Mr. Biden through the rest of the new five-year term that Mr. Erdogan won in May.

“And with the forthcoming elections, I would like to take this opportunity to also wish you the best of luck,” he said, prompting Mr. Biden to laugh.

“Thank you very much,” Mr. Biden replied, saying he looked forward to cooperating “for the next five years.”

Mr. Erdogan’s about-face on Sweden is consistent with his political style: He often doubles down on policies he expects to strengthen him and then unapologetically throws them out once their worth has diminished, analysts said.

The turn also comes as Turkey’s economy teeters under the pressures of runaway inflation, heavy debt and the soaring recovery costs from devastating earthquakes. Maintaining good relations — and economic ties — with Western nations could help Mr. Erdogan weather the crises, just as he has sought to preserve trade with Russia.

Ever since Sweden applied to join NATO last year, Mr. Erdogan has been the primary stumbling block, accusing Sweden of harboring dissidents whom Turkey considers terrorists, and vowing to let the country join NATO only after it cracked down on them.

And Sweden responded, a win for Mr. Erdogan. Sweden has hardened its antiterrorism laws, amended its Constitution and agreed to extradite a number of people Turkey has requested.

Turkey won a few more concessions on Monday: Sweden agreed to continue its counterterror efforts and to increase economic cooperation with Turkey.

Mr. Erdogan also made a surprise effort on Monday to link Turkey’s long-stalled bid to join the European Union with Sweden’s quest to join NATO — but that did not bear any tangible fruit. Other issues may have played a larger role in changing Mr. Erdogan’s mind, analysts said.

Turkey has been trying to buy F-16 fighter jets and other military equipment from the United States, but the deal has been held up by Congress, where some lawmakers said they would not approve it unless Turkey approved Sweden’s NATO bid.

On Tuesday, Jake Sullivan, Mr. Biden’s national security adviser, said the president supported the transfer but had not directly linked it to Turkey’s Sweden decision.

He said Mr. Biden had a “long, detailed, constructive” phone call with Mr. Erdogan on Sunday, during which he told the Turkish president that Sweden had done what it was supposed to do to join NATO. That call, Mr. Sullivan said, left Mr. Biden confident that Mr. Erdogan would agree. Mr. Sullivan said that he had also called his Swedish and Turkish counterparts in recent days.

The U.S. officials tried to convince Mr. Erdogan that it supported his request for F-16 fighter jets, but maintained that the White House needed support from Congress to make the transfer. The officials told their Turkish counterparts it would be easier to sway Congress if Mr. Erdogan first agreed to allow Sweden to join NATO, according to a U.S. official who spoke on the condition of anonymity to discuss the internal conversations.

Even if the two issues were not explicitly linked, a number of Turkish analysts said that White House guarantees that Mr. Biden would work to push the deal through Congress likely influenced Mr. Erdogan.

Mr. Erdogan was probably also aware that NATO allies’ patience was beginning to wear thin, and that holding out on Sweden’s membership bid would make relations even worse.

“Turkey made the assessment that the potential additional benefits they could gain by prolonging the process were no longer worth the pressure that Turkey would need to face,” said Ozgur Unluhisarcikli, the director of the Ankara office of the German Marshall Fund of the United States.

The move, he said, also appeared to be part of an effort by Mr. Erdogan to shift Turkey away from Russia and toward the West. That would be significant, if sustained, he said.

“Turkey and Russia did not and do not have a love affair,” he said. Instead, they engage in “competitive cooperation” and tend to get closer when Turkey feels distant from the West.

Mr. Unluhisarcikli gave two recent indications of Turkey moving away from Russia: the decision to allow fighters from Ukraine’s Azov Regiment to return to Ukraine from Turkey last weekend, angering the Kremlin, and Mr. Erdogan’s failure to stand unambiguously by Mr. Putin as Wagner mercenary forces marched toward Moscow in June.

“Erdogan might have assessed that putting all the eggs in Putin’s basket is not a good idea,” he said.

On the flip side, allowing Sweden into NATO and working out the F-16 agreement could allow Mr. Erdogan to warm up what has been a cool relationship with Washington.

Mr. Biden has kept Mr. Erdogan at arm’s length since he entered the White House. During his electoral campaign, he characterized Mr. Erdogan as anti-democratic and spoke about supporting the Turkish opposition.

The two heads-of-state have met during Mr. Biden’s presidency, but he is the first U.S. president not to host Mr. Erdogan in the White House since the Turkish leader began his national political career in 2002.

Ben Hubbard reported from Istanbul and Zolan Kanno-Youngs from Vilnius, Lithuania. Gulsin Harman contributed reporting from Istanbul.