The Afghan was an asylum seeker who had arrived in Germany as an unaccompanied minor.
The teen had received a word that a friend had died in his home country days before. He vowed in a note that he would 'take revenge on these infidels'.
Germany has been on edge after suffering two attacks claimed by the Islamic State group in July.
Germany's Thomas Lurz became the only man to have struck gold in all four open water events when he battled through the pain of the final sprint to edge the 25-km race at the world championships in Barcelona on Saturday.
The pistol-wielding attacker, identified by Munich Police Chief Hubertus Andrae as a dual national, was later found dead of an apparent self-inflicted gunshot wound to his head.