The fatalities included 132 students and nine school employees, the military's director of public information, Gen. Asim Bajwa, told a press conference.
Another 122 students were wounded, as well as nine of the soldiers who retook the school from the insurgents.
More than 900 people were inside the compound at the start of the assault, Bajwa said.
Seven Taliban fighers dressed in army uniforms entered the school through a back door shortly before midday, police spokesperson Seid Wali said.
The attackers hurled grenades and fired burst of gunfire as they went from classroom to classroom, Wali added.
One of the students, a 14-year-old boy, told The Express Tribune that two men burst into his classroom and began shooting indiscriminately.
The Pakistani army launched an operation to liberate the school, which serves grades 1-10, but progress was slow as the troops had to contend with explosives planted inside by the attackers.
Soldiers eliminated the last of the insurgents by 6:20 p.m., authorities said.
Television stations broadcast scenes of chaos around the school and the sounds of explosions and gunfire were clearly audible in the background.
The attackers never planned to take hostages and were simply out to kill as many people as possible, Gen. Bajwa said.
After securing the school, the military embarked on a anti-insurgent sweep across Peshawar and the surrounding province of Khyber Pakhtunkhwa.
Pakistan's main Taliban group, known as the TTP, claimed responsibility for the attack and said it was in reprisal for the what the militants claimed was the targeting of their families by the military.
A counterinsurgency operation six months ago in the Khyber and North Waziristan areas left more than 1,100 insurgents dead, according to Pakistan's army.
Prime Minister Nawaz Sharif called the assault on the school "a national crisis", declared three days of mourning and convened a meeting Wednesday in Peshawar with leaders of all parties represented in the Pakistani parliament.
US President Barack Obama condemned the attack, as did Indian Prime Minister Narendra Modi and Afghanistan's Ashraf Ghani.
Malala Yousafzai, the Pakistani girl who was shot by the Taliban in 2012 for her outspoken advocacy on behalf of girls' education and went on to share this year's Nobel Peace Prize, said she was "heartbroken by this senseless and coldblooded act of terror in Peshawar."