Silvestre Varela came off the bench to score an 87th-minute.
Portugal went ahead after 24 minutes when Pepe glanced in a header from a corner and doubled their lead in the 36th with simple finish by Helder Postiga from a low Nani cross, Reuters reported.
Nicklas Bendtner pulled one back in the 41st minute and, after Cristiano Ronaldo had missed a glorious chance to make it 3-1, looked to have secured a point for the Danes when he headed his second after 80.
The final and resulting goal was scored by Varela, three minutes after he entered.
Portugal, Denmark and Germany have three points each. Earlier, Germany beat Portugal 1-0.