Stay up to date with the latest news
Subscribe to our telegram channel
Pepe scored the opener on 24 minutes and Helder Postiga doubled the lead of Portugal on 36 minutes.
Sunderland forward Nicklas Bendtner scored twice on 41 and 81 minutes to level the score.
But substitute Silvestre Varela netted the winner for Portugal to keep their hopes for qualification alive.