The youth, who was also armed with three knives, smashed the top-floor room at Handsworth Wood Girls School in Birmingham during the incident.
Armed police surrounded the school while negotiators persuaded him to give himself up after releasing most of the pupils.
When he eventually surrendered, six of the 13- and 14-year-old pupils and a teacher remained in the classroom. The gun was found to be a fake.
Elaine Foster, the headteacher, said: 'No pupils or staff were injured but we are of course liaising with the police and . . . the education department to arrange counselling for those affected.'
Superintendent John Dwyer, of West Midlands Police, said: 'The man walked into the classroom and held the two teachers and 35 children hostage at gunpoint.
'Fortunately no one was injured and everyone is now out safely. But they are all suffering with severe symptoms of shock. We are unclear about a motive at this stage.'
A youth was in custody last night, helping police with their inquiries.