Quaresma chipped over a lovely cross from the right and Simao darted in front of central defender Luisao to hit the ball first time low into the corner past keeper Helton to put Portugal in front in the 82nd minute.
Carvalho hooked home a cross in the final minute to secure the victory that gave Portugal coach Luiz Felipe Scolari his second win over his home country since a 2-1 victory in Porto in 2003.
It was Dunga's first defeat in six internationals as Brazil coach since taking over from Carlos Alberto Parreira after last year's World Cup. It leaves the Brazilians, who have won 11 of their 17 meetings with Portugal, still looking for their first win over their old rivals since 1989.
Brazil had the better of the first half, forcing Portugal into isolated counter-attacks and the South Americans came close to going ahead two minutes before the interval when centre back and captain Lucio hit the bar from close in following a free kick.
Lively midfielder Elano served notice of what to expect when he brought a save from Ricardo in the fourth minute, the keeper tipping his free kick from the right wing over the bar.
But Elano faded in the second half and was unable to repeat his two-goal performance against Argentina when Brazil beat their arch-rivals 3-0 at the same ground in September.
Kaka, dominant in the first half, also faded and Brazil lost their grip on midfield after the break with Portugal gradually making more and more inroads into the defence but not before several first half scares.
Ricardo saved with his feet from Rafael Sobis after a quarter of an a hour and the striker was just wide with a header near the half hour following Elano's neat one-two with right back Maicon and cross to the far post.
There was good quality play from end to end and no shortage of passion despite the "friendly" tag to the delight of a crowd of nearly 60,000.