Police found the gunman's body lying in a smoke-filled classroom after he had run amok with two sawn-off shotguns, injuring a pregnant teacher, a caretaker and pupils.
Five of the wounded were in a serious but stable condition.
"We do not know whether the attacker died because of the explosives or whether he shot himself," state prosecutor Wolfgang Schweer told reporters.
The gunman was named as Sebastian Bosse, a former student at the Scholl Geschwister high school in Emsdetten near the Dutch border, who had announced the impending attack on his two Internet homepages.
"He was a loner who was severely unhappy with his life. The statements he made could be interpreted as pointing to a spectacular suicide," Schweer said.
"He had two homepages and he also left behind a letter."
The websites, which were shut down by police following the attack, featured pictures of the black-haired teenager posing with rifles fitted with telescopic sights.
He had also posted video footage of himself running through a forest, firing a gun, in a sequence entitled "Good-bye Humanity".
According to the n-tv news channel, Bosse had written on one of his homepages: "The only thing I learned in school is that I am a loser. I hate these people and their kind. They must all die."
Schweer said the caretaker, who was shot in the stomach, and four students -- three boys and girl -- suffered serious gunshot wounds but their lives were not in danger.
The drama began just before 9:30 am (0830 GMT) when the gunman ran into the grounds of the school wearing a black gas mask and started shooting, Schweer said.
The teacher and the caretaker were injured when they tried to confront him.
The police chief in the nearby city of Muenster, Hubert Wimber, said Bosse then holed up in a room on the second floor of the school building and set off several smoke bombs.
"Smoke was pouring out of the room. We thought the school was on fire," he said.
"The police found him lying on the floor with two sawn-off shotguns next to him. They did not fire a single shot."
He said members of a bomb disposal unit were trying to defuse explosives on Bosse's body and were checking that his car, which was parked in the street outside the school, had not been booby-trapped.
Police said explosives had also been found in the garage of his parents' home.
Wimber said four policemen required hospital treatment for severe smoke inhalation.
A pupil told n-tv that he had seen the former student arrive at the school and open fire.
"He was wearing black army boots and he was shooting all around him," the unidentified student said.
During the stand-off, most pupils at the school ran to safety in a neighbouring building and field, while others were evacuated after police had located the gunman.
Pupils at the school, which is situated in a middle-class neighbourhood, told reporters they had found their alienated former schoolmate "scary".
They said Bosse had been an avid fan of the computer war game "Counterstrike" and when the shooting began, they initially thought it was a game.
When they realised the firing was real, they ran for their lives.
Headmistress Carola Keller said Bosse's sister was still a student at the school.
In the worst school shooting in recent history in Germany, a former pupil massacred 16 people before turning the gun on himself in April 2002 in the eastern city of Erfurt.
Copyright AFP (Agence France-Presse), 2006