MADRID — Turkey agreed Tuesday to lift its opposition to Sweden and Finland joining NATO, a breakthrough in an impasse clouding a leaders’ summit in Madrid amid Europe’s worst security crisis in decades triggered by the war in Ukraine.

After urgent top-level talks, alliance Secretary-General Jens Stoltenberg said “we now have an agreement that paves the way for Finland and Sweden to join NATO.”

Russia’s invasion of Ukraine has prompted Sweden and Finland to abandon their long-held nonaligned status and apply to join NATO. But Turkish President Recep Tayyip Erdogan had blocked the move, insisting the Nordic pair change their stance on Kurdish rebel groups that Turkey considers terrorists.

Finnish President Sauli Niinisto said the three countries’ leaders signed a joint agreement after talks Tuesday.

Turkey said it had “got what it wanted” including “full cooperation ... in the fight against” the rebel groups.

The agreement comes at the opening of a crucial summit dominated by Russia’s invasion of Ukraine. President Joe Biden and other NATO leaders arrived in Madrid for a summit that will set the course of the alliance for the coming years.

Stoltenberg said the meeting would chart a blueprint for the alliance “in a more dangerous and unpredictable world.”

“To be able to defend in a more dangerous world we have to invest more in our defense,” Stoltenberg said. Just nine of NATO’s 30 members meet the organization’s target of spending 2% of gross domestic product on defense. Spain, which is hosting the summit, spends just half that.

Top of the agenda for leaders in meetings Wednesday and Thursday is strengthening defenses against Russia and supporting Ukraine.

Biden, who arrived with the aim of stiffening the resolve of any wavering allies, said NATO was “as united and galvanized as I think we have ever been.”

Diplomats and leaders from Turkey, Sweden and Finland earlier held a flurry of talks in an attempt to break the impasse over Turkey’s opposition to expansion.

Erdogan is critical of what he considers the lax approach of Sweden and Finland toward groups that Ankara deems national security threats, including the Kurdistan Workers’ Party, or PKK, and its Syrian extension. American support for Syrian Kurdish fighters in combating the Islamic State group has also enraged Turkey for years.

Turkey has demanded that Finland and Sweden extradite wanted individuals and lift arms restrictions imposed after Turkey’s 2019 military incursion into northeast Syria.

Ending the deadlock will allow NATO leaders to focus on their key issue: an increasingly unpredictable and aggressive Russia.

A deadly Russian missile strike Monday on a shopping mall in the central Ukrainian city of Kremenchuk was a grim reminder of the war’s horrors. Some saw the timing — as Group of Seven leaders met in Germany and just ahead of NATO — as a message from Moscow.

Ukrainian President Volodymyr Zelenskyy, due to address NATO leaders by video Wednesday, called the strike on the mall a “terrorist” act.

On Tuesday, rescuers combed through the charred rubble of the shopping mall that authorities said was struck when more than 1,000 afternoon shoppers and workers were inside.

In addition to the 18 killed, authorities said 59 were wounded. Another 21 people are still missing, Ukrainian Interior Minister Denis Monastyrsky said.

The attack recalled strikes earlier in the war that hit a theater, a train station, and a hospital.