Substitute Silvestre Varela kept alive Portugal’s hopes of qualifying from Group B with a late strike to spare the blushes of some of his more illustrious team-mates in Lviv.
Varela smashed home the winner having come on just four minutes earlier after Real Madrid star Cristiano Ronaldo had missed two straightforward 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 Arsenal striker Nicklas Bendtner appeared to have secured a point as he 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.
Having wrestled the early initiative from Denmark, who lost Niki Zimling to an early calf injury, Ronaldo failed to test goalkeeper Stephan Andersen with a free-kick from wide on the left but their next two set-pieces were more productive.
Miguel Veloso’s long-range free-kick saw Postiga’s shot deflected wide but from the resulting 24th-minute corner Portugal took the lead.
Nani’s inswinging cross picked out the run of the unmarked Pepe and the Real Madrid defender’s glancing header beat Andersen at his near post.
Boosted by the goal Portugal continued to press forward and doubled their lead 12 minutes later. Simon Kjaer’s clearance from Fabio Coentrao’s cross only reached Joao Pereira who quickly transferred the ball to Nani and his square pass was smartly converted by Postiga.
Christian Eriksen registered Denmark’s first shot on target to draw a save from Rui Patricio but the Portugal goalkeeper was helpless to prevent the Danes from pulling one back four minutes before half-time.
Jakob Poulsen’s hanging cross to the far post picked out Michael Krohn-Dehli who headed back across goal for Bendtner to nod home his 19th international goal in his 50th appearance.
Portugal should have re-established their cushion five minutes into the second half after Kjaer’s misjudgement of Nani’s crossfield pass sent Ronaldo racing clear but he shot too close to Andersen.
Another mistake, this time by Eriksen, gave the ball to Postiga and only a perfectly-timed tackle from Daniel Agger prevented the striker breaking into the penalty area.
William Kvist drilled a long-range shot narrowly wide, Bendtner scuffed one off target while substitute Tobias Mikkelsen, on after Dennis Rommedahl’s hamstring injury, started to find joy down the right as Denmark began to push harder for the equaliser.
Ronaldo’s frustrating tournament continued as he should have wrapped up victory 12 minutes from time when put clean through by Nani but shot wide with only Andersen to beat.
He was punished for that mistake as Bendtner’s far-post header from Lars Jacobsen’s deep cross proved too powerful for Patricio to keep out.
However, there was a further twist three minutes from time when Varela smashed home with his second attempt to convert Coentrao’s left-wing cross.