Police said information so far indicated the suspect entered the classroom mid-morning and "suddenly attacked the victim" in the school in a working class suburb of the northern port city of Hamburg.
"He apparently used a knife and stabbed the 17-year-old," they said in a statement. "The victim died a little later at the scene."
The suspect was detained in the classroom and did not resist arrest, they said, adding that the reason for the attack was not yet known.
Hamburg's mayor Olaf Scholz reacted with shock.
"A shadow has fallen over our city," he said in a statement. "We are appalled by an act of violence which leaves us stunned."