Portugal's hopes of reaching the Euro 2012 quarter-finals boosted after beating Denmark 3-2
Substitute Silvestre Varela's late strike boosted Portugal's hopes of reaching the Euro 2012 quarter-finals.
Pepe's thumping near-post header put Portugal ahead, before Helder Postiga smashed in a close-range volley.
But two headed goals from striker Nicklas Bendtner brought the Danes level and looked set to leave Paulo Bento's men with an uphill task to advance from Group B.
Then with three minutes left, Varela's right-foot shot won it for Portugal.
Portugal, coming into the game on the back of defeat by Germany, needed at least a draw to stand any chance of extend their record of reaching the knockout stage in their last five European Championships.
In contrast, Denmark headed into the Lviv clash brimming with confidence following their opening 1-0 win against the highly fancied Dutch. And they held another psychological advantage over their opponents having finished above them in qualifying groups for both the 2010 World Cup and Euro 2012.
Portuguese boss Bento was heavily criticised by his country's press for his side's defensive tactics against Germany - indeed his side had failed to score in three of their previous six games heading into this match - and adopted a more adventurous approach against the Danes.
Cristiano Ronaldo, looking to add to his 62-goal season for Real Madrid, was often the focal point of Portugal's attack but it was an off-day for the 27-year-old, who missed several chances in front of goal, most notably a glorious one-on-one chance seconds before Bendtner's leveller.
It was his Bernabeu team-mate Pepe who broke the deadlock when the dithering Danish defence allowed the defender to amble to the front post and meet Joao Moutinho's left-wing corner.
With Ronaldo struggling to find top gear, Portugal relied on his ex-Manchester United colleague Nani to open up the Danish defence in the first half.
And Nani was the architect as former Tottenham striker Postiga became just the sixth player to score in three European Championship finals.
Denmark failed to close down Portuguese attacks on either flank before Nani picked out Postiga at the near post with a precise square pass.
Victory for Morten Olsen's men would have put them on the brink of a quarter-final place following their surprise win over the misfiring Dutch.
And the Scandinavians were given a lifeline shortly before the break when Portugal's defence switched off to allow Michael Krohn-Dehli to nod Lars Jacobsen's deep cross back across goal for Bendtner to score the easiest of close-range headers.
It meant the Arsenal striker marked a landmark 50th cap in style and has now scored in each of his five appearances against the Portuguese.
Bendtner looked the most likely of the Danish players to find an equaliser in the second half and duly obliged when he met Jacobsen's right-wing cross at the far post.
But there was to be a further twist as Varela smashed in a right-footed shot from outside the Denmark area to spare Ronaldo's blushes.