Police later said the attacker had been detained and had been led out of the school and into a waiting car. According to the Lifenews website, he initially shot one officer and then opened fire at others who arrived at the scene.
"The person who took 20 people and a teacher hostage is a student in the upper classes at the same school. He has been neutralized and all the students have been freed," Interior Ministry spokesman Andrei Pilipchuk said on state TV.
(Read more: More security concerns raised for Sochi)
"One policeman was fatally wounded during the operation and died in hospital, and a teacher at School No. 263 was also killed," he said.
The shooting sent dozens of students scurrying out the school while a police helicopter landed in a snow-covered field outside.
It came with Russia in the global spotlight four days before it hosts the 2014 Winter Olympics in the Black Sea resort city of Sochi.
Follow us on Twitter: @CNBCWorld