Brazil had to rely on a 90th-minute Kaka penalty to overcome plucky Egypt and claim a barely-deserved win to start their Confederations Cup defence in Bloemfontein.
The South Americans looked to be on their way to an easy win in Group B after first-half goals from Kaka, Luis Fabiano and Juan opened up a 3-1 lead at the break.
But a fragile first-half defensive performance meant they never fully looked in control of the contest and two goals in as many minutes soon after the re-start, from Mohamed Shawky and first-half scorer Mohamed Zidan, had the Africans dreaming of a memorable win.
Those hopes were dashed a minute from time, however, when defender Ahmed Al Muhamadi handled a Lucio shot on the line and was given his marching orders.
Kaka, who had been a largely peripheral figure, then stepped up to coolly convert the spot-kick and deny Egypt.
Brazil had looked set for an easy afternoon when new Real Madrid signing Kaka put them ahead after five minutes.
Kaka, who signed for the Spanish giants for a reported fee of 68million euros last week, twice juggled the ball past a pair of would be defenders on the edge of the area before calmly slotting past goalkeeper Essam El Hadary.
It was an ominous beginning from the Selecao but, to Egypt's credit, they rallied immediately and were level four minutes later.
The Pharaohs' path to goal was made easier by some lax Brazilian marking as they failed to track Mohamed Aboutrika's run down the right flank.
The winger had all the time he needed to pick out Zidan, who headed home at the back post after his markers had also failed to keep a watchful enough eye.
It was a moment of lost concentration that would typify Brazil's defensive performance but such was their attacking threat it seemed that would be of little concern.
Indeed, they took only three minutes to regain their lead when Luis Fabiano deftly guided an Elano free-kick past a diving El Hadary.
Juan then missed a good chance for a third when he headed over from an Elano corner with less than 30 minutes played.
Egypt were being outplayed but a second goal was not beyond them as they were continually allowed space to break forward.
Veteran Hassan Ahmed passed up two good chances for the Africans as he failed to control a clever throughball from Zidan, before taking too long to shoot in the area and allowing a slow-retreating defence to relieve him of the ball.
Brazil coach Dunga was cutting an agitated figure on the touchline and ironically it was one of his central defenders, Juan, who offered him some relief when he headed in an Elano corner after 37 minutes.
Egypt gave warning they were not willing to lie down in injury-time when Hosni Abd Rabou narrowly headed over Sayed Moawad's left-wing cross. It was a warning Brazil would not heed as they conceded their two-goal advantage in two frantic minutes soon after the break.
Again Egypt were allowed space down the flank and Aboutrika drove forward before pulling the ball back to Shawky, who drilled a shot low past Julio Cesar with 54 minutes on the clock.
A minute later, the Brazilian goalkeeper was again fetching the ball out of his net.
This time the Egyptians took a more direct route to goal as Aboutrika flicked a pass behind the defence for Zidan, who took a touch before thrashing his shot past an exposed Cesar.
It was a stunning salvo from the Egyptians, who last week lost 3-1 to Algeria in a World Cup qualifier, and their more fancied hosts failed to find a response.
The game looked set to be heading for a draw until the final moments when Lucio met a corner with a clean volley that Al Muhamadi kept out with an outstretched arm.
The defender then writhed around in pain, claiming the ball had hit his face, but referee Howard Webb was not fooled, showing him a straight red and awarding Brazil a penalty.
Kaka then made no mistake from the spot as he stroked the ball into the right-hand corner.