Germany lost for the first time in 23 matches as Manchester City forward Gabriel Jesus scored the only goal to give Brazil the win in Berlin.
This was the first full meeting between the two nations since Germany humiliated Brazil 7-1 in the 2014 World Cup semi-final in Belo Horizonte.
Jesus put Brazil ahead with a powerful diving header late in the first half.
He missed another chance with a header and Germany’s Julian Draxler had a late shot tipped over as Brazil held on.
It meant Joachim Low’s side, the reigning world champions and the team top of the world rankings, lost for the first time since a 2-0 defeat by France in the semi-finals of Euro 2016.
Since then, they had won 16 times and drawn six before this match at the Olympiastadion.
Although it was only a friendly, this win will mean a lot to a Brazilian side that has been haunted by their failure to win the World Cup almost four years ago in their own country and the humiliating nature of their exit at the hands of Germany.
Before the match, Brazil coach Tite, who was appointed boss in 2016, said: “This has a huge psychological meaning – no-one needs to fool themselves about that.
“The 7-1 is like a ghost, people still talk about it. The more you talk about it, the less this ghost disappears. The wound is still open and the match in Berlin is a part of the process of closing it.”
Tite backed up his words by naming what looked to be the strongest side that he had available, with only Neymar absent as he recovers from a fractured metatarsal.
Germany, on the other hand, made six changes from the side that drew 1-1 with Spain on Friday and made five substitutions during the match, compared to only one by Brazil.
The visitors went ahead in the 37th minute when Jesus’ powerful diving header was fumbled over the line by third-choice goalkeeper Kevin Trapp.
That had come only seconds after Jesus had missed a chance by shooting over and he was wasteful again in the second half, heading wide with the goal unguarded after Trapp had come off his line and misjudged a corner.
However, the German did redeem himself with a fine diving save to deny ex-Tottenham man Paulinho’s well-struck effort.
Manchester City pair Leroy Sane and Ilkay Gundogan failed to make an impact for the hosts, and both were substituted.
Germany finally produced a shot on target in the 92nd minute, with Brazil goalkeeper Alisson pushing over Draxler’s effort.
Brazil beat Germany in the 2016 Olympic Games football final, but this was the first senior international between the sides since 2014 and Brazil’s victory helped them ease the wounds from four years ago.