Substitute Silvestre Varela kept alive Portugal's hopes of qualifying from Euro 2012 Group B with a late strike in the 3-2 victory over Denmark in Lviv.
The Porto forward smashed home the winner having come on just four minutes earlier after Real Madrid star Cristiano Ronaldo had missed two opportunities to put the result beyond Denmark.
Portugal's chances had looked increasingly remote with just three minutes remaining after they squandered a two-goal lead.
Goals from Pepe and Helder Postiga had Paulo Bento's side coasting but Nicklas Bendtner appeared to have secured a point for Denmark.
Bendtner continued his remarkable scoring spree against the Portuguese with both of his country's efforts to take his tally to six in five matches against their European rivals.
The win gave Portugal their first points of the tournament and ensured they go to the last match still in with a chance of reaching the quarter-finals.
Both teams sit on three points and there is all to play for in the final Group B games after Germany beat Holland 2-1 in the day's other game.
Portugal manager Paulo Bento praised his side's character and resilience after they claimed victory.
"Conceding an equaliser so late, we showed great strength of character to keep going after the win," said Bento.
"We should have killed the game off earlier, we had enough chances," added the 42-year-old, referring to Ronaldo's two missed chances.
Denmark coach Morten Olsen admitted conceding a late winner was devastating for morale but was not unhappy with the performance.
"It's a horrible feeling to concede so close to the end and end up losing the game," he said.
"We struggled to find our rhythm but we really improved in the second half and in the last 10 minutes we came into our own.
"I was proud of the way we played against the Netherlands (winning 1-0) but in a sense I'm prouder here of our second-half performance.