Brazil survived a torrid start to their Confederations Cup campaign in South Africa and despite an impressive first-half performance from Elano needed a last-minute penalty to squeeze past stubborn opponents Egypt 4-3.
City midfielder Elano had a hand in two of the Brazil goals that gave the Copa America holders a comfortable 3-1 interval lead only for the African champions to stun their fancied rivals with two second-half goals in 52 seconds.
Both Elano and City team-mate Robinho had been substitute by the time Kaka coolly converted the late spot kick that also cost Egypt substitute Ahmed Al Muhamadi a red card from English referee Howard Webb for handling on the line.
Egypt, ranked 40th in the world, had no answer to Kaka's classy opening goal in the fifth minute as Real Madrid's expensive new signing demonstrated the close control and composure that tempted the Spanish giants.
Mohamed Zidan levelled in the ninth minute, but then Elano delivered one of his precision free-kicks for Luis Fabiano to head Brazil back in front on 12 minutes before planting a corner on Juan's head for a third goal on 37 minutes.
But the Bloemfontein crowd, many supporting their fellow Africans, erupted after the break when Middlesbrough striker Mohamed Shawky snatched one back from distance then Zidan swiftly equalised with a left-footer on 55 minutes.
Brazil coach Dunga replaced first-half hero Elano after 62 minutes and at the same time brought off Robinho, who had worked hard with little reward, for Milan's young goalscoring prospect Alexander Pato.
They will no doubt hope for less of a struggle in their next Group B tie against the USA on Thursday.