Portugal's Silvestre Varela came off the bench and smashed home an 87th-minute goal to give his side a nail-biting 3-2 victory over Denmark on Wednesday and keep them on course for the Euro 2012 quarter-finals.
With three minutes left Portugal looked set to drop two more points after another flat tournament performance in which they let slip a two-goal lead against the Danes, for whom Nicklas Bendtner equalised in the 80th minute.
Then Varela, who had only come on in the 84th minute, was given time and space to take two swings at the ball and whacked it into the net from eight yards out. He immediately disappeared under a pile of jubilant team mates and substitutes.
"Their equaliser was unfair at that stage of the match. We could have won the game more comfortably and with less suffering," said Portuguese coach Paolo Bento, who had been under increasing pressure after just one win in seven games.
Even so, Portugal's unusually careless defence could have conceded a third goal had not Lasse Schone fired over the bar in stoppage time.
Denmark and Portugal have three points each in Group B with one game left. Leaders Germany and the Netherlands play later on Wednesday.
Portugal came into the game having lost three consecutive European Championship matches stretching back to Euro 2008.
They easily took a 2-0 lead in a first half marked by sparkling play from winger Nani but looked to have thrown it away when Bendtner evaded the excellent Pepe and headed home a Lars Jacobsen cross via the post to make it 2-2.
Denmark - Portugal 0-1
Denmark - Portugal 0-2
Denmark - Portugal 1-2
Denmark - Portugal 2-2
Denmark - Portugal 2-3
The Danes, who beat the Dutch 1-0 in the first game, now need to pick up at least a point against Germany in the final match on Sunday and hope other results go their way.
"It's a horrible feeling to lose so close to the end ... the Portuguese were a bit luckier. Maybe they were a bit better," coach Morten Olsen told reporters.
Although Nani had an excellent game, making one goal and generally tormenting the Danish defence, Cristiano Ronaldo disappointed in a big international game yet again.
The speedy winger, who has a reputation for under-performing at tournaments, wasted two good chances, firing straight at Andersen in the 50th minute after breaking away and taking an excellent pass from Nani and screwing a shot wide in the 78th when it looked much easier to score.
Bento, who said the player had been "inefficient", was lucky others were sharper in front of goal.
Denmark have a good recent record against Portugal but looked largely toothless early on and it was little surprise when the Portuguese, abandoning the caution which marked their 1-0 loss to Germany in the first game, took the lead.
Pepe easily lost his marker from a corner by Joao Moutinho and powered a header past keeper Stephan Andersen in the 24th minute.
Portugal 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 before the match that Denmark would be able to contain 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 with a close-range header after Michael Krohn-Dehli looped a header over keeper Rui Patricio. The goals were his fourth and fifth in four competitive games against Portugal.
The Danes, who had defended well in their opening 1-0 win over the Dutch, were not helped by the loss of midfielder Niki Zimling and winger Dennis Rommedahl with muscle injuries.