ISTANBUL—Turkey’s parliament approved Sweden’s entrance to NATO, removing one of the last remaining obstacles to a historic expansion of the alliance in response to Russia’s invasion of Ukraine.

The vote on Tuesday night likely ends a nearly two-year-long diplomatic standoff between Turkey and Western powers that began when Turkish President Recep Tayyip Erdogan threatened to block both Sweden and Finland’s entry into the organization. Finland joined the North Atlantic Treaty Organization last year after Turkey dropped its opposition.

Now Sweden only needs Hungary’s parliament to give its approval to join the alliance. Hungary had pledged not to be the last country to approve Swedish membership, but has become the final holdout.

“We are very much looking forward to becoming members of NATO,” Swedish Foreign Minister Tobias Billström said in an interview. “We do expect white smoke from Budapest as well.”

NATO Secretary-General Jens Stoltenberg welcomed Turkey’s vote and urged Hungary to ratify accession quickly. “Sweden has fulfilled its commitments. Sweden’s membership makes NATO stronger and all of us safer,” he said.

Sweden’s membership would complete a significant enlargement of NATO that shifts the balance of power in Europe, adding thousands of soldiers along with naval power and warplanes. Sweden and Finland applied for NATO membership in 2022, seeking to shore up their security in response to Russia’s assault on Ukraine. The decision ended a decadeslong policy of managing an uneasy relationship with Russia by choosing not to align with NATO in order avoid provoking the Kremlin.

The addition of Finland and Sweden would effectively allow the alliance to operate across an area that spans the 1,000-mile stretch from the Baltic Sea to the Arctic. With Sweden in NATO, Russia would become the sole non-NATO country bordering the Baltic Sea, forcing its warships to pass near NATO-controlled shores en route to the Atlantic.

“It will send a message that NATO is unified,” said Gulru Gezer, a former senior Turkish diplomat who worked in the U.S. and Russia. “It shows that Turkey is an important member of NATO and it acts accordingly.”

Russia has both criticized and played down the two countries’ efforts to join NATO.

Erdogan is expected to sign Sweden’s accession protocol into law in the coming days.

Turkey’s final approval for Swedish membership in NATO also removes a significant source of tension with the U.S., helping the Biden administration achieve one of its most important strategic goals in its response to the war in Ukraine.

The move could help ease relations with Washington and potentially pave the way for a $20 billion sale of new F-16 warplanes sought by Turkey. Erdogan has tried to link approval for Sweden’s NATO membership with the arms deal, which officials on both sides view as a way to solidify relations between a pair of allies that have been at odds in recent years in part over Erdogan’s warm relations with Russian President Vladimir Putin.

The Biden administration must seek approval for the F-16 sale from leaders in Congress, some of whom have expressed concerns about the deal in part because of criticism of Erdogan’s human-rights record.

Policy analysts said Sweden’s decision to join NATO marked a historic shift in the country’s security policy.

“It’s been the top foreign policy priority for this government. It’s a really big step,” said Paul Levin, director of the Stockholm University Institute for Turkish Studies.

Erdogan in 2022 threatened to veto both Sweden and Finland’s membership in NATO, demanding that both countries take a harder line against the Kurdistan Workers’ Party (PKK), an insurgent group that the U.S., Turkey, and EU all label a terrorist organization. Turkey accused both Sweden and Finland of allowing members of the PKK to reside in those countries and demanded that they extradite them. Sweden disputed the claim. Finland vowed to resolve the issue through diplomacy.

In response, Sweden passed a new counterterrorism law, and both countries, along with Canada and the Netherlands, took steps to ease arms sales to Turkey.

The Turkish government has long been critical of Western governments’ stance on Kurdish groups, especially after the U.S. chose to join with PKK-linked Syrian Kurdish militias in the war against Islamic State in Syria in recent years. The U.S. makes a distinction between the PKK and its Syrian branch, which is a part of the U.S.-led military coalition.

Erdogan agreed with other NATO leaders last year to approve Sweden’s NATO application, but the Turkish government again delayed parliamentary approval when new tensions arose between Turkey and Western countries over Israel’s war with Hamas in Gaza. Erdogan, the leader of an Islamist party and president of an influential Muslim-majority country, has been critical of the Biden administration’s support for Israel amid the rising death toll in Gaza.

Erdogan has caused other tensions within NATO in recent years, incurring American sanctions after he decided to buy a Russian air defense system in 2017 and expanding Turkey’s economic ties with Russia, causing concerns that the country is helping the Kremlin to evade Western sanctions.

“It will create positive momentum, but it won’t change U.S.-Turkish relations overnight,” said Gezer of the vote on Sweden.

Hungary’s Prime Minister Viktor Orbán said on Tuesday that he invited Sweden’s prime minister to negotiate over the country’s NATO accession. Hungary, which like Turkey has relatively close relations with Russia, previously said it would approve Sweden’s entrance to the bloc but hasn’t set a date for a vote in parliament.

—Daniel Michaels and Elvan Kivilcim contributed to this article.