Monroeville police Chief Doug Dole says the boy turned over the gun after bringing it to Ramsey Elementary School in his backpack Wednesday morning.
The school is part of the Gateway School District about 15 miles east of Pittsburgh. Spokeswoman Cara Zanella says the situation was dealt with quickly before a lockdown became necessary.
Police say the boy's home was searched after the incident, though details of what was found - and whether his parents were home or knew about the weapon - were not immediately available.
The boy will be disciplined and school officials have decided to ban backpacks for elementary students for the rest of the year.