However, the Leipzig-based federal administrative court found that the right to pray even at school was guaranteed by religious freedom under the constitution.
In the case of the 18-year-old pupil, who took his school to court, it justified the ban at his Berlin high school because the issue of praying had already sparked conflict among Muslim pupils.
The court said the school, in Berlin's ethnically diverse Wedding district, was right in stopping him from praying as "sometimes very severe conflicts" had broken out among Muslim pupils over the interpretation of the Koran.
Capping a more than two-year legal battle, it ruled that a pupil "is not entitled to perform prayer during school outside of class when this can disrupt the running of the school."
The pupil, identified only as Yunus M., is the son of a German Muslim convert. He had insisted on his basic right to religious freedom after the school principal told him praying at school was not permitted.
With an estimated four million Muslims living in Germany, the case sparked interest after two earlier regional court rulings first found in favour of the boy, only then to be overturned.
It began in November 2007 when Yunus M. and several other pupils laid down their jackets in the school corridor to pray during a break from class.
The following day the principal informed the boy and his parents that praying was not permitted on the grounds of the school which has students of about 30 different nationalities and nearly all major religions. She feared for the peaceful running of the school, she said.
Yunus M. had argued that because prayer times depend on the rising and setting of the sun, he had no other choice during the winter but to pray around midday while at school.
News weekly Der Spiegel said that the consequences of Wednesday's ruling
would be closely watched nationwide.
"Yunus M. kicked off proceedings which could write legal history," it said in its online edition.
Tilman Nagel, an expert in Islam who appeared as a witness at an earlier court hearing, said that postponing midday prayers was acceptable if there was a good reason.
He also argued that the Islamic ritual of praying undertaken with other people was very different to the Christian private act of praying, and was thus disruptive in a public space.
Germany has grappled with the thorny issue of the integration of its Muslim population since it signed a "guest worker" pact with Turkey 50 years ago.
This has sometimes fuelled tensions, with a former central banker publishing a runaway bestseller last year saying Germany was being made "more stupid" by millions of purportedly undereducated and unproductive Muslim migrants.