Portugal substitute Silvestre Varela scored a dramatic 87th-minute winner to give his side a 3-2 win over Denmark in a pulsating Euro 2012 Group B match on Wednesday.
Portugal looked to have thrown a two-goal lead away when Danish striker Nicklas Bendtner scored the second of two close-range headers to equalize in the 80th minute.
But Varela gave the Portuguese the win they badly needed when he smashed home from close range.
Portugal had taken a 2-0 lead in the first half after good work by Nani. Defender Pepe lost his marker from a corner and powered a header past keeper Stephan Andersen in the 24th minute.
They doubled their lead in the 36th minute when striker Helder Postiga got in front of defender Simon Kjaer far too easily to turn home a Nani pass from close range.
It was a bad moment for Kjaer, who had boasted Denmark would be able to contain winger Cristiano Ronaldo, and he had to make a desperate clearance in the 44th minute to stop Nani setting up another goal.
Bendtner scored his first in the 41st minute, nodding home after Michael Krohn-Dehli sent a looping header over keeper Rui Patricio. The goals were his fourth and fifth in four competitive games against Portugal.
The result means Denmark and Portugal have three points with one game left. Leaders Germany also have three and play the pointless Netherlands later on Wednesday.
"Our character and identity were important, we didn't start playing differently when they equalized and we got a win which was fair but we could have won more comfortably without that suffering at the end," said Portugal coach Paulo Bento.
Ronaldo had another poor game and missed two good chances, shooting straight at Andersen in the 50th minute and screwing a shot wide in the 78th when it looked easier to score.
Denmark have a good recent record against Portugal but looked largely toothless and it was little surprise when Portugal, who had lost three consecutive European Championship games, took the lead.
Pepe was their best player in the 1-0 defeat by Germany last Saturday and looked good again.
The Danes, who had defended brilliantly in their opening 1-0 win over the Dutch, were not helped by the loss of midfielder Niki Zimling and winger Dennis Rommedahl to injury.
"Of course it's a horrible feeling to lose so close to the end," said Denmark coach Morten Olsen. "Maybe they were a bit better, they had a few free runs... that was our mistake.
"It's hard to be in these positions and sometimes you're the lucky one and today I think we were a little bit unlucky.
"...we have a few injuries but we'll pick it up before the game against Germany (on Sunday)."
(Editing by Ed Osmond)