Portugal's Pepe calls for the referee's attention during the Euro 2012 soccer championship Group B match between Denmark and Portugal in Lviv (Photo: AP)
A stunning goal by substitute Silvestre Varela three minutes from time saw the 2004 finalists take the points after they had let slip a 2-0 lead as Danish striker Nicklas Bendtner drew the Danes level with two headers.
Goals from Pepe - with a superb header - and Helder Postiga, with his 20th goal for his country, had given the Portuguese a perfect start.
However, a series of terrible misses by their star player Cristiano Ronaldo had threatened to prove very costly until Varela stepped up to save them just three minutes after coming on.
The win puts Portugal level on points with Denmark, who beat the Dutch in their opening game, while Germany top the table and take on Holland later on Wednesday.
For Pepe it was not so much about personal satisfaction with regard to his goal but more the overall benefit to the team.
"I had the luck to score against Turkey at the last Euro," said the 29-year-old Real Madrid defender.
"I was able to help once again my team, but the most important thing is the teame. We showed a real willingness to sacrifice ourselves in order to win this match."
The Danes had the first chance in the fifth minute as the ball deflected to highly-rated youngster Christian Eriksen inside the area but Pepe did well to block his goalbound shot.
The Portuguese managed to put very little together in the opening 20 minutes save once when Ronaldo drifted in from the wing into the centre and played the ball to Raul Meireles.
Meireles found the Real Madrid star - just five goals in 15 tournament matches - in space but he produced a woeful shot and tried to blame the pitch as he looked ruefully down at it afterwards.
However, he was not found wanting when he delivered his corner in the 24th minute as Pepe came steaming in to head home, the ball flying in over Danish midfielder William Kvist's head at the near post.
Ronaldo appeared to take heart out of that and he was unfortunate to see an on-target freekick take a deflection off a Danish defender in the 32nd minute.
But he did not have to wait long for his side to double their lead.
His former Manchester United team-mate Nani was the creator, passing the ball into the box where Postiga got a yard on his marker Simon Kjaer and put the ball away with a sharp finish.
The Danes, though, got themselves back in the match four minutes before half-time as a smart header across the goal from Michael Krohn-Dehli found Bendtner unmarked and he made no mistake with a close range header to make it five successive games with the Portuguese that he has found the net.
Ronaldo, though, should have restored the two goal advantage soon after the start of the second-half as put clear he was one on one with Danish 'keeper Stephan Andersen, but the latter blocked his shot with his legs.
The Danes still found plenty of fight with Kvist going close with a rasping shot.
Bendtner was in lively form and worked himself some space in the 72nd minute to set himself up nicely but he sent his shot well wide of the goal.
Ronaldo's woes in front of goal continued as Nani's superb through ball found him in acres of space and from about eight yards out and with just Andersen to beat he put it wide - provoking jeers and whistles in the stands.
That miss looked to have been extremely costly as Bendtner rose superbly to head home Lars Jacobsen's excellent cross from the right.
However, the drama was not over as Fabio Coentrao hared down the left flank and crossed into the box. Varela swung at the ball with his left foot and miskicked but showing remarkable composure connected with his favoured right foot and lashed home.
(For more sports news and updates, follow Ahram Online Sports on Twitter at @AO Sports and on Facebook at AhramOnlineSports)