Michael Krohn-Delhi gave Denmark a 13th-minute lead, while Nicklas Bendtner doubled their advantage soon after the hour mark. Cristiano Ronaldo's last-minute goal will prove scant consolation for the Portuguese, who must now negotiate the play-offs if they are to play in Poland and Ukraine.
Denmark scored with their first clear chance of the game when young playmaker Christian Eriksen found Krohn-Delhi who finished from the edge of the area.
Nani sought a response for Portugal, but his shot was charged down by Andreas Bjelland before Krohn-Delhi went close to a second at the other end.
Portugal launched a concerted effort to draw level before the break, but Jorge Rolando missed the target when well placed, while Ronaldo brought a pair of saves out of Thomas Sorensen and Raul Meireles fired wide.
Krohn-Delhi had another chance early in the second half, but blazed over before Portugal went on the charge again, with Jorge Carlos Martins missing a pair of chances. Denmark capitalised as they doubled their lead, Bendtner poking home from Dennis Rommedahl's cross in the 63rd minute.
Portugal attacked straight from the restart, but Martins missed, Ronaldo headed over and Nani fired wide as they looked short of a cutting edge up front.
Eriksen fluffed a chance to finish Portugal off 17 minutes from time, allowing Portugal another roll of the dice as Bruno Alves headed narrowly wide from Meireles' cross and the Chelsea man then fired wide from a good position.
Bendtner's 84th-minute header was tipped over before Rommedahl failed with a close-range effort, and those missed set up a nervy finish after Ronaldo rifled in a free-kick a minute into stoppage time.
There were four added minutes in total, but after Nani was caught offside on the final attack of the match, Portugal - who would have advanced with a draw - were forced to settle for second. Ronaldo became Portugal's fourth highest goalscorer of all time with his 30th international goal.