Artem Dovbyk scored in added time in extra time as Ukraine edged out 10-man Sweden at Hampden to set up a Euro 2020 quarter-final with England.
Oleksandr Zinchenko smashed Ukraine in front with a powerful drive but a deflected shot from Emil Forsberg levelled matters before half-time.
Sweden lost Marcus Danielson to a red card in extra time after a VAR check.
And, with penalties looming, Dovbyk - one of 12 substitutes used - headed a dramatic winner from close range.
Ukraine had crept through from the group phase with three points and a negative goal difference, while Sweden topped their section with seven points, ahead of Spain.
However, the match in Glasgow was an even contest throughout, with both sides out on their feet by the final whistle.
With so many changes in personnel and a sending off, there was no rhythm or shape in extra time.
The Swedes were twice temporarily down to nine men after head knocks and Ukraine replacement Artem Besedin lasted just 10 minutes before he was on the receiving end of Danielson's high challenge.
The Sweden defender, who got his foot to a dropping ball before catching his opponent, was initially booked but referee Daniele Orsato upgraded to red after reviewing the incident on the pitch-side monitor.
There was little in the way of goalmouth action after Danielson's 99th-minute dismissal until Zinchenko swung in a cross from the left and Dovbyk slid in to score his first international goal.
After the game, Sweden manager Janne Andersson said: "It felt like we would score eventually, but unfortunately we didn't. The red card decided the game and we had to fight for it. It's incredibly difficult and tough result to take. We had some long throw-ins but we didn't do much in extra time. It's a bitter defeat.
"I was really surprised it was a free kick at first, but my colleagues in the stands said it was a red card. It's an experienced referee. I think it's probably right."
The Man City defender said after the match: "First of all, I'd like to thank supporters. The victory is for you. This victory I'd like to dedicate you to Besedin, he received a horrible injury - when I came back to the locker room, I was beside myself. I'm sure he'll come back stronger."
Ukraine will now face England, who beat Germany earlier in the day, in the last-8 in Rome on Saturday.
"I said several times before, it doesn't matter what team you play for and if you're part of a team, it doesn't matter where you play, you have to give your all," Zinchenko added. "I will do everything possible and impossible for it."