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.
The Porto forward 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.
Hero: Varela celebrates his late winner
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 Anderson 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 Anderson at his near post.
Cities & Stadiums
The Top 3 Teams of Previous Tournaments
|2008||Spain||Germany||Russia / Turkey|
|2004||Greece||Portugal||Netherlands / Czech Republic|
|2000||France||Italy||Netherlands / Portugal|
|1996||Germany||Czech Republic||France / England|
|1992||Denmark||Germany||Netherlands / Sweden|
|1988||Netherlands||Soviet Union||Italy / West Germany|
|1984||France||Spain||Denmark / Portugal|
|1972||West Germany||Soviet Union||Belgium|