A 17-year-old wielding a Beretta 9 mm pistol burst into classrooms at his former high school and gunned down students — some of whom died with their pencils still in hand — in a rampage that ended with 15 dead before he took his own life, authorities said.

There was no immediate indication of motive, but the gunman's victims were primarily female: eight of nine students killed were girls, and all three teachers were women. Three men were killed later as the suspect fled.

"I heard two shots and then screaming," said a 15-year-old student who gave her name only as Betty. "At first I thought it was a joke, but then someone called 'Run, run!' and I saw students jumping out of the windows and took off running."

Click here for photos.

The gunman — dressed in all black — took students in the first classroom completely by surprise on Wednesday, evidenced by the morbid scene that awaited the first officers to arrive, said regional police director Ralf Michelfelder.

"Children were sitting at their tables, with pencils still in their hands, their heads fallen over on the table," he said. "Most of them had shots in their head — it must have all happened in seconds."

Police identified the gunman only as Tim K. But the name on his parent's home was Kretschmer and local media identified him as Tim Kretschmer.

The dark-haired teen, shown wearing glasses in pictures on German television, apparently took the weapon from his father's collection of 15 firearms along with a "multitude of ammunition," police said. His father was a member of the local gun club and kept the weapons locked away except for the pistol, which was kept in the bedroom.

Police said the suspect was a German teen who was a below-average student at the school of about 1,000 pupils, but managed to graduate last year. A sister still attends the school.

"He was lower than average, and he wasn't engaged in school events," Michelfelder said.

Fabienne Boehm, 12, said she recently met the shooter through a friend, and that he had shown her a note three weeks ago that he then sent to his parents.

"He wrote to his parents that he's suffering and he can't go on," she said.

Boehm told The Associated Press that the shooter claimed fellow students at the high school had mocked him, and that teachers there ignored him.

A 17-year-old who would give only his first name, Aki, said he had been studying this year with the shooter at a private business school, and described him as a quiet, reserved person.

Aki said the two played poker together, both in person and online, as well as a multiplayer video game called "Counter-Strike" that involves killing people to complete missions. "He was good," Aki said.

Teenagers were sobbing violently and clinging to each other as they left a church service to the victims Wednesday.

Police received an emergency call from the school at 9:33 a.m. The first officers responded about two minutes later, said Baden Wuerttemburg state interior minister Heribert Rech. They heard shots on the second floor and ran upstairs, catching a glimpse of the suspect on a staircase, Rech said.

He fired a shot at the police and then fled, killing his last victims in the school — two teachers — on his way out, Rech said.

"Our officers were very quick," Rech said. "Through the immediate police intervention they were able to prevent a further escalation of the crime."

After fleeing the school, the suspect ran into downtown Winnenden, a town of 28,000, where he shot two people walking by a psychiatric clinic, killing one and wounding the other, police said.

The gunman then hijacked a car and forced the driver to head south while threatening his life from the back seat, triggering a land and air manhunt involving 700 police officers and four helicopters, according to Stuttgart prosecutors.

The driver swerved off the road to avoid a police checkpoint and managed to escape, while the suspect fled from the car into an industrial area in the town of Wendlingen, about 24 miles (40 kilometers) from Winnenden.

He entered a car dealership, where he shot and killed his final victims — a salesman and a man shopping for a car — and then went back outside, prosecutors said.

He opened fire on police swarming the area, who shot back and hit the suspect, who fell wounded to the ground, Michelfelder said.

But he got back up, reloaded his weapon, and fled into what turned out to be a dead-end street. Police found him there dead, having apparently shot himself in the head.

Two police officers suffered serious, but not life-threatening, injuries.

The death toll was close to that of Germany's worst school shooting.

In the 2002 shooting, 19-year-old Robert Steinhaeuser shot and killed 12 teachers, a secretary, two students and a police officer before turning his gun on himself in the Gutenberg high school in Erfurt, in eastern Germany.

Steinhaeuser, who had been expelled for forging a doctor's note, was a gun club member licensed to own weapons. The attack led Germany to raise the age for owning recreational firearms from 18 to 21.

German Chancellor Angel Merkel called the shooting "a horrific crime."

"It is hard to put into words what happened today, but our sadness and sympathy goes out to the victims' families," Merkel said.

The European Parliament, meeting in Strasbourg, France, stood in silence for a minute, to honor the victims.

"It is our task as responsible politicians in the European Union and, indeed, all the member states to do our utmost that such deeds can be prevented," said EU assembly president Hans-Gert Pottering, a German.