Hungary snatched a 100th-minute winner to beat Scotland 1-0 and put themselves in contention for a last-16 spot at the European Championship while condemning the Scots to yet another failure at a major tournament.
Kevin Csoboth's breakaway goal with almost the last kick of the match ensured Hungary finished third in Group A with three points while Scotland, yet to reach the knockout phase of either the Euros or World Cup, end in last place on two points.
Hungary must wait to see whether they will qualify for the last 16 in one of the places reserved for the best four third-placed finishers.
Their progress now rests on results from other groups going their way over the next three days. However, for now Marco Rossi's side will revel in their dramatic victory that rescued their hopes just as they thought they were heading home.
"It was unbelievable. We never achieved nothing in the past without big, big suffering. Today was no different. I think that we played the match that we needed to play," Rossi said.
"We created many options, many situations. We also left some (chances) to them. But overall, I think if I say that it was a deserved win, I'd be right," he added.
The match was a scrappy affair, with both teams struggling to exert any kind of control despite each being desperate for a win to continue their stay in Germany.
Steve Clarke's Scotland did dominate possession in the opening 25 minutes but could not fashion any clear-cut chances. Indeed, they had no shots at all in the first half despite having 61 per cent of the ball. They ended the match without a shot on target.
Hungary, meanwhile, grew into the game and began threatening on the break and making quick transitions, with captain Dominik Szoboszlai in particular threatening from set pieces.
The midfielder's freekick to the far post in the 41st minute was met by an unmarked Willi Orban but his poor header clipped the top of the bar from close range.
Che Adams had Scotland's first shot shortly after the break but he blasted it over.
The game was paused for a lengthy period in the 68th minute after a horrible clash between Scotland goalkeeper Angus Gunn and Barnabas Varga from another Szoboszlai freekick.
Varga appeared to be knocked out cold and needed urgent treatment, and Hungary players erected blankets around him as a screen while they looked in distress, with Szoboszlai in tears.
The forward was carried off the field on a stretcher to applause from the crowd. He was taken to Stuttgart Hospital, the Hungarian Football Federation said, adding that he was in a stable condition.
"That gave us that extra bit, obviously we don't ever want to see that on a football pitch but thankfully he's conscious now," Hungary midfielder Callum Styles said.
Scotland had a strong appeal for a penalty dismissed in the 79th minute when substitute Stuart Armstrong ran on to the ball in the box but fell under contact from Orban. However, the referee did not consider it a foul.
The game become more chaotic as both teams sought a winner.
Gunn did well to save from Szoboszlai's shot at the near post in the first minute of stoppage time, while Csoboth struck the post a minute later from the right when bearing down on goal.
Scotland rushed forward and McTominay slid in at the near post to meet a low cross but the ball missed the target.
It was from a Scottish corner that Hungary turned into a rapid counter-attack up the pitch. A long cutback across the box fell to Csoboth who slotted the ball in, sending Hungary fans delirious and players from their bench running over in celebration.
Scotland could only look on dejected as their bid to make history ended in dramatic fashion to the latest goal ever scored at a European Championship in regulation time.
"Tonight's devastating, the lads are absolutely gutted. Yeah but it's up to us to pick them up and that will happen slowly but surely," Scotland captain Andy Robertson said.
"But thank you to the country because we felt everyone behind us, and we knew the excitement back home -- sorry for letting you down."