One of his teachers, Arfa Iftikhar, was forced into hiding after a furious mob stormed Farooqi Girls' High School in the eastern city of Lahore over a piece of homework she set.
Blasphemy is an extremely sensitive issue in Pakistan and allegations of insulting Islam can prompt violent outbursts of public anger, even when unproven.
The lawyer, Jawad Ashraf, had asked for bail on the grounds the headteacher had no role in the incident.
"The principal of the school can only be held responsible for negligence and has no direct role in the blasphemy incident, he should be released," he said.
About 10 clerics from local mosques gathered at the court to oppose the headteacher's release.
At the same time, around 2,000 students, parents and teachers gathered at the school to demand its reopening, carrying placards and chanting slogans which included: "Release our principal."