President Volodymyr Zelenskiy hailed as "superheroes" the senior Ukrainian commanders, including those who led the dogged defence of Mariupol, who were freed by Russia as part of a prisoner swap involving almost 300 people, including foreigners.
Under the terms of the deal, which Turkey helped broker, 215 Ukrainians - most captured after the fall of the port city - were released on Wednesday. In exchange, Ukraine sent back 55 Russians and pro-Moscow Ukrainians.
Ten foreigners were also freed following mediation by Saudi Crown Prince Mohammed bin Salman, who has maintained close ties with Russian President Vladimir Putin.
Among them was Briton Aiden Aslin, who had been sentenced to death by a court in the self-proclaimed Donetsk People's Republic, who said they were "out of the danger zone".
The timing and size of the swap came as a surprise. Earlier in the day Putin had announced a partial troop mobilisation in Russia in an apparent escalation of the conflict that began in February. Pro-Russian separatists had said last month that the Mariupol commanders would go on trial.
"Five superheroes have been exchanged for 55 of those who deserve neither compassion nor pity," Zelenskiy said in a night-time address that welcomed the broader release as "a victory for the country".
Russia's defence ministry said on Thursday 55 of its servicemen had been returned in the largest prisoner exchange with Ukraine since the start of the war.
It said the fighters - from Russia's armed forces and its proxies in the Russian-backed separatist regions of eastern Ukraine - had been transported to Russia by a military plane and were undergoing health checks.
The Russian-installed separatist head of the Donetsk People's Republic said pro-Kremlin Ukrainian politician Viktor Medvedchuk had been freed. Medvedchuk, a personal friend of Putin, headed a banned pro-Russian party in Ukraine and was facing treason charges.
Ukrainians cheered the swap.
"We live for the soldiers. They are our pride, our glory, our joy – our boys. I only hope everyone can be freed. And that our mother-Ukraine is freed too," 55-year-old Kyiv resident Tamara Herasymenko said.
The five senior commanders freed include Lieutenant Colonel Denys Prokopenko, and his deputy, Svyatoslav Palamar, both of the Azov battalion, which did much of the fighting in Mariupol and is lionised in Ukraine.
Also freed was Serhiy Volynsky, commander of the 36th Marine Brigade, who in April issued a video message from where fighters were holed up in bunkers below the vast steel works in Mariupol. "This is our appeal to the world. It may be our last. We may have only a few days or hours left," he said at that time.
The three men had helped lead the weeks-long resistance before they and hundreds of Azov fighters surrendered in May to Russian-backed forces.
Zelenskiy held a video call with the men after their release from captivity.
The five commanders will stay in Turkey until the end of the war, and Zelenskiy thanked Turkish President Tayyip Erdogan for his help after what he said had been a long and difficult fight to secure their release.
The Azov Regiment is reviled by Putin's Kremlin as a band of Russia-hating neo-Nazis. The battalion denies allegations of fascism, Nazism and racism and says Ukrainians from various backgrounds serve in Azov, which is now fully integrated into Ukraine's armed forces.
The eventual capture of Mariupol, on the Sea of Azov, was a strategic prize for Russia, though its forces had reduced the port city to a wasteland littered with bodies after nearly two months of siege and heavy bombardment.
It meant Russia had a secure overland route linking the Crimean peninsula, which Moscow annexed in 2014, with mainland Russia and parts of eastern Ukraine already held by separatists.
The foreigners released included Aslin, fellow Briton Shaun Pinner and Moroccan Brahim Saadoun who had been sentenced to death in June after being captured fighting for Ukraine - a sentence condemned by Kyiv and Britain.
"We just want to let everyone know we're now out of the danger zone and we're on our way home to our families," Aslin said in a video on Instagram. Pinner added: "By the skin of our teeth."
They thanked everyone who had supported them, comments echoed by Zelenskiy. Riyadh brokered the arrangement whereby the 10 foreigners were flown to Saudi Arabia.
"At one time we were discussing this with (former British prime minister) Boris Johnson, he inquired for support with our American friends ... We saved their lives. All of them will come home with the mediation of Saudi Arabia," Zelenskiy said.
Also freed were three other Britons, two Americans, a Croatian, and a Swedish national. The five UK prisoners had arrived in Britain, non-profit organisation Presidium Network was reported as saying by the BBC on Thursday.
The freed U.S. citizens were Alexander Drueke, 39, and Andy Huynh, 27, both from Alabama, who were captured in June while fighting in eastern Ukraine.
(Reporting by Valentyn Ogirenko in Kyiv, Aziz El Yaakoubi in Riyadh and David Ljunggren in Ottawa; Editing by Alison Williams and Gareth Jones)