Taliban gunmen stormed a military-run school in the north-western Pakistani city of Peshawar, killing at least 141 people, mostly children, before Pakistani officials declared a military operation to clear the school over.
Prime Minister Nawaz Sharif condemned the assault and rushed to Peshawar to show his support for the victims.
As darkness fell on the area, officials said they had cleared the school of militants.
A Pakistani military spokesman, Asim Bajwa, said 141 people died in the attack - 132 children and nine staff members.
He declared the operation over and said the area had been cleared. An additional 121 students and three staff members were wounded.
He said seven attackers, all wearing explosives vests, all died in the assault.
It was not immediately clear if the militants were all killed by the soldiers or whether they blew themselves up, he said.
Mr Bajwa described an assault that seemed designed purely to terrorise the children rather than take anyone hostage to further the militant group's aims.
"Their sole purpose, it seems, was to kill those innocent kids. That's what they did," he said.
The horrific attack, claimed by the Tehreek-e-Taliban, a Pakistani militant group trying to overthrow the government, sent dozens of wounded flooding into local hospitals as terrified parents searched for their children.
"My son was in uniform in the morning. He is in a casket now," said one parent, Tahir Ali, as he came to the hospital to collect the body of his 14-year-old son Abdullah. "My son was my dream. My dream has been killed."
The attack began in the morning when the gunmen entered the school and started shooting at random.
Army commandos quickly arrived at the scene and started exchanging fire with the gunmen. Students wearing green school uniforms could be seen fleeing the area on Pakistani television.
Outside the school, two loud booms of unknown origin were heard coming from the scene in the early afternoon, as Pakistani troops battled with the attackers. Armoured personnel carriers were deployed around the school grounds, and a Pakistani military helicopter circled overhead.
Pakistani television showed soldiers surrounding the area and pushing people back. Ambulances streamed from the area to local hospitals.
The prime minister vowed that the country would not be cowed by the violence and that the military would continue with an aggressive operation launched in June to rout militants from the North Waziristan tribal area.
"The fight will continue. No one should have any doubt about it," Mr Sharif said.
Mr Bajwa said that 1,099 students and staff were registered at the school.
It is part of a network of schools run by the Pakistani military around the country. The student body is made up of both children of military personnel as well as civilians.
A government official, Javed Khan, said most of the students appeared to be civilians rather than children of army staff. But analysts said the militants are likely to have targeted the school because of its military connections.
"It's a kind of a message that we can also kill your children," said Pakistani analyst Zahid Hussain.
One of the wounded students, Abdullah Jamal, said that he was with a group of 8th, 9th and 10th graders who were getting first-aid instructions and training with a team of Pakistani army medics when the violence began for real.
When the shooting started, Jamal, who was shot in the leg, said nobody knew what was going on in the first few seconds.
"Then I saw children falling down who were crying and screaming. I also fell down. I learned later that I have got a bullet," he said, speaking from his hospital bed.
Another student, Amir Mateen, said they locked the door from the inside when they heard the shooting but gunmen blasted through the door anyway and opened fire.
In a phone call to reporters, Taliban spokesman Mohammed Khurasani claimed responsibility for the attack, saying the suicide bombers carried it out to avenge the killings of Taliban members at the hands of Pakistani authorities.
Peshawar has been the target of frequent militant attacks in the past but has seen a relative lull recently.
The Pakistani military launched the operation in the nearby North Waziristan tribal area in June, vowing that it would go after all militant groups that had been operating in the region. With the launch of the operation, security officials and civilians feared retribution by militants but until Tuesday, a widespread backlash had failed to materialise.
Tuesday's attack calls into question whether the militants have been crippled by the military or will be able to regroup.
This appeared to be the worst attack in Pakistan since a 2007 suicide bombing in the port city of Karachi killed 150 people.
The violence also underscored the vulnerability of Pakistani schools, which was dramatically exposed in the attack two years ago on Malala Yousafzai, a Pakistani girl shot in the head by a Taliban gunman outside her school in the Swat Valley for daring to speak up about girls' rights. She survived, becoming a Nobel Prize laureate and global advocate for girls' education but out of security concerns has never returned to Pakistan.
Malala said in a statement: "I am heartbroken by this senseless and cold-blooded act of terror in Peshawar that is unfolding before us.
"Innocent children in their school have no place in horror such as this.
"I condemn these atrocious and cowardly acts and stand united with the government and armed forces of Pakistan whose efforts so far to address this horrific event are commendable.
"I, along with millions of others around the world, mourn these children, my brothers and sisters - but we will never be defeated."
US President Barack Obama said: "The United States condemns in the strongest possible terms today's horrific attack on the Army Public School in Peshawar, Pakistan.
"Our hearts and prayers go out to the victims, their families, and loved ones. By targeting students and teachers in this heinous attack, terrorists have once again shown their depravity.
"We stand with the people of Pakistan, and reiterate the commitment of the United States to support the government of Pakistan in its efforts to combat terrorism and extremism and to promote peace and stability in the region."
UN Secretary-General Ban Ki-moon condemned the "blood-curdling attack" as "an act of horror and rank cowardice".
The UN chief told the UN Security Council at the start of a meeting that no cause or grievance can justify the killing of at least 126 people, mainly children.
"Schools must be safe and secure learning spaces, and getting an education is every child's right," Mr Ban said. "Going to school should not have to be an act of bravery."
He pledged UN support to Pakistan in its fight against terrorism and urged the government to bring the perpetrators of the attack to justice.
"The hearts of the world go out to parents and the families who have lost loved ones," Mr Ban said.