The hosts took the lead just before the break when Neymar marked his fourth goal of the tournament with a memorable free kick that struck the crossbar before nestling in the back of the net.
The goal perhaps came against the run of play following a dominant German display in the first half, but the visitors came out fighting in the second period and they found a route back into the contest through Max Meyer, who made it 1-1 following Jeremy Toljan's cut-back.
Brazil produced much of the attacking play in the remaining minutes, but they were unable to force another breakthrough as the contest went into extra time.
Neither side were able to score the all-important goal during the extra 30 minutes, leaving the medals to be decided on penalties, despite the best efforts of Felipe Anderson to beat Timo Horn in the German goal.
As expected, the shootout proved to be an intense affair and it was Germany who made the vital error when Nils Petersen saw his penalty saved, leaving Neymar to slot away Brazil's final kick in order to seal a historic gold medal for his country.