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.