Brazil should have had the outcome decided by the interval given the amount of chances they created but Germany struck twice in the second half, despite going down to 10 men in the 63rd minute.
Brazil found plenty of space from the outset and Matheus Cunha could have put them ahead in the fifth minute but his low shot at the near post was kept out by keeper Florian Mueller.
Two minutes later though the Brazilians had the lead when Richarlison burst through the inside right channel and after his first shot was parried he made no mistake with his second attempt, drilling the ball past Mueller.
The German defence was all at sea and Mueller was busy again to foil Richarlison as the forward attempted to round him and then kept out Claudinho’s effort from the loose ball.
It was no surprise when Brazil doubled their advantage with Guilherme Arana, in acres of space down the left, taking his time to pick out Richarlison in the middle and the Everton striker’s powerful header flashed past Mueller.
Richarlison completed his hat-trick with a superbly taken solo goal, curling the ball into the far corner after being set up by Matheus Cunha.
It should have been 4-0 shortly before the interval but Cunha saw his penalty kick well saved by Mueller after Benjamin Henrichs handled the ball inside the box.
The chances kept coming for Brazil after the break but Stefan Kuntz’s Germany side did manage to get a goal back, against the run of play, with a 57th minute Nadiem Amiri effort which keeper Santos should have kept out.
A miserable night for the Germans got worse when Max Arnold was sent off in the 63rd minute, rather harshly, for a second yellow card.
Germany gave themselves a lifeline six minutes from the end when Ragnar Ache powered in a header from a David Raum cross to make it 3-2.
But with Germany pushing forward, Brazil caught them on the break in stoppage time, Paulinho finishing off a counter-attack with a fine shot into the top corner.
The match was a repeat of the gold medal game from Rio five years ago and was played at the venue where Brazil beat Germany in the World Cup final in 2002.