The 18-year-old stormed a McDonald's outside the Olympia mall in Munich on Friday evening, police Chief Hubertus Andrae said.
The shooting in the nation's third-largest city left 16 others wounded, with children among the casualties.
At first, authorities believed there were three attackers involved and warned residents to avoid public places, sending terrified shoppers running to strangers' homes nearby.
Police put the city on lockdown and and halted public transportation.
Shortly after, surveillance video and witness statements concluded it was one attacker, according to the police chief. He said the gunman was found dead of a self-inflicted gunshot wound.
'He's ... killing the children'
A witness who was in the restaurant said her son was in the bathroom with the gunman.
"That's where he loaded his weapon," said Lauretta, who only wanted to be identified by her first name.
She said she saw many children casualties.
"I hear like an alarm and boom, boom, boom ... and he's still killing the children. The children were sitting to eat. They can't run."
Lauretta said she heard the gunman say, "Allahu Akbar," or "God is great" in Arabic.
"I know this because I'm Muslim. I hear this and I only cry," she said.
'You will get it.'
Witness Huseyin Bayri said he was riding his bike outside the restaurant when he saw the attacker yelling.
"I heard a scream at first: 'You shitty ... foreigners. I am German. You will get it,'" Bayri said.
"And then the first shots were fired. And a boy of about 14 or 17 years of age ... collapsed, fell to the floor."
Bayri jumped off his bike, crawled toward the wounded teenager and hid next to him. He tried to reassure him as he pressed on his gunshot wounds to stop the bleeding.
"The boy asked for my help. I tried to talk with him, asked him his name, his age, if he has a girlfriend," Bayri said. "I tried to make sure that he looked at me, that his eyes are open."
The teen pleaded with Bayri to call an ambulance, but died just before it got there.
From eatery to mall
After the shooting, the gunman moved across the street to the mall, which is near the site of the 1972 Summer Olympics where Israeli athletes were killed by Palestinian attackers.
Lynn Stein, who works in the mall, described a chaotic scene when gunfire erupted.
"People were very confused, and they were running and they were screaming," she said.
She saw someone lying on the floor of a store who appeared to be either dead or injured.
"There's a woman over them, crying," she said.
Photos and video posted on social media showed a profanity-filled verbal exchange between a man on the top level of a parking garage and another one on a balcony.
The exchange, recorded on two camera phones, captured an intense conversation that ends in gunfire. The man who appears to be a shooter said insulting things about Turks, did not espouse jihadist ideology and spoke with a German accent.
Shooter lived in Munich
While police did not identify the shooter, they said it was an 18-year-old German-Iranian who had lived in Munich for at least two years.
He was unknown to police and his motives are unclear, authorities said. No group has claimed responsibility.
"We have to investigate everything via third person as we cannot question the perpetrator now," Andrae said.
Chancellor Angela Merkel called a security council meeting for Saturday.
Germany increased security nationwide, with enhanced police patrols deployed to the Austria-German border and special forces from Bavaria and surrounding federal states came to Munich as reinforcements.
Organizers also canceled a weekend beer festival, citing security.
Europe has been on high alert following a recent spate of terror attacks.
This week, a teenager who said he was inspired by ISIS stabbed passengers on a German train before police shot him dead.
In France, a man killed 84 people last week when he drove a large truck through a crowd celebrating Bastille Day in Nice.