ISLAMABAD, Pakistan — The bloody siege of an elite army high school Tuesday by Taliban gunmen, which killed at least 141 students and teachers, was an apparent retaliation for a major recent army operation after years of ambivalent policies toward the homegrown Islamist militants.

The mass targeting of children, in a military zone in the northwestern city of Peshawar, drew condemnation from around the world, as well as from across Pakistan’s political and religious spectrums — a rare display of unity in a country where Islamist violence is often quietly accepted and sometimes defended. The attack was also condemned by Taliban leaders in Afghanistan.

Some analysts suggested that after years of suicide bombings and attacks on markets, mosques, hotels and military bases, the insurgents had finally gone too far, and that widespread public outrage over this attack might signal a decisive turn in the nation’s — and the government’s — reluctance to fully take on the Taliban.

The massacre was the most intimate assault ever against Pakistan’s military, the nation’s most respected and powerful institution. The only comparable incident was in December 2009, when a small group of assailants penetrated army headquarters in the garrison city of Rawalpindi and killed more than 30 people praying at an army mosque.

The death toll on Tuesday also rivaled one of the highest in Pakistan in recent years, when suicide bombings in 2007 killed about 150 people in Karachi during celebrations to welcome former prime minister Benazir Bhutto back to Pakistan after years in self-exile. Bhutto was assassinated soon after.

Yet even when previous attacks have drawn strong condemnation and vows of action from military officials, Pakistan’s military and intelligence establishment has remained deeply ambivalent about taking on the domestic Islamist forces and has often been accused of playing a double game in its partnership with the West in the war on terrorism.

One chief reason is that such extremist groups have long acted as proxies in Pakistan’s rivalry with India, an issue that trumps all others for Pakistan’s security leaders and that has long been seen as a far greater threat than Islamist militants. Terrorist attacks are routinely decried as the work of unknown foreign hands.

Pakistan’s civilian leaders, for their part, have long deferred to the army in security and foreign policy, and they have also been reluctant to act against Islamist violence, for fear of alienating the nation’s deeply religious Muslim masses and organized groups.

“Despite this national tragedy, I don’t see any chance of the nation as a whole building an anti-terrorism narrative,” said Aftab Ahmed Khan Sherpao, a veteran Pakistani legislator from the northwest.

He noted that a variety of religious and political leaders have “deep sympathy” for the militants.

“For now they may tone down their support,” he said, but in time they will “start showing their true colors again.”

The army, however, has always been carefully attuned to public opinion, and Tuesday’s attack provoked a remarkably swift, broad and emphatic outpouring of revulsion and anger. News channels showed grim scenes of dead children in hospital beds, many still wearing green school uniforms, and of weeping mourners carrying hastily made pine coffins out of hospitals in Peshawar.

“Today is the saddest day of the history of our nation,” said Haniyah Siddiqui, 18, who was shopping in the port city of Karachi. “It is high time to make up our mind to fight terrorists and eliminate them in toto, not just mourning or condemning the tragic incident.”

‘Pakistani Prime Minister Nawaz Sharif, who rushed to Peshawar, denounced the assault as a “cowardly act” and vowed to maintain military action “until the menace of terrorism is eliminated” from Pakistan. “The nation needs to get united and face terrorism,” he added. “We need unflinching resolve against this plague.”

Malala Yousafzai, the Pakistani teenager and Taliban attack survivor who recently won the Nobel Peace Prize for promoting girls’ education, said from England that she was “heartbroken” by “these atrocious and cowardly acts” but vowed that even as she and millions mourn the students’ deaths, “we will never be defeated.”

Her denunciation was echoed by Hafiz Mohammad Saeed, the leader of Pakistan’s Jamaat-ud-Dawa Islamist movement, whose followers were blamed for a 2008 terrorist siege on the Indian city of Mumbai. Saeed said the attack was “carried out by the enemies of Islam. It is open terrorism. . . . These are barbarians operating under the name of jihad.”

Even the Afghan Taliban, which operates separately from the Pakistani group but shares a religious agenda, took the unusual step of indirectly condemning the attack. A statement from spokesman Zabiullah Mujahid said, “The intentional killing of innocent people, women and children are against the basics of Islam, and this criterion must be considered by every Islamic party and government.”

The Pakistani Taliban quickly asserted responsibility for the attack, saying it was to avenge Pakistan’s sweeping military operation in June in North Waziristan, part of a tribal region that straddles the Pakistan-Afghanistan border. The group had been warning for months that it would take revenge.

Hasan Askari Rizvi, a military analyst, said the attack was “unprecedented,” even in a country that has experienced thousands of terrorist attacks over the past decade. He said the Taliban appears to be growing more desperate as the military operations continue.

“Now they are attacking the soft targets,” Rizvi said.

But Mohammad Khorasani, a spokesman for the Pakistani Taliban, said the attack was “a gift for those who thought they have crushed us in their so-called military operation in North Waziristan.” He said such opponents were “always wrong about our capabilities. We are still able to carry out major attacks, and today was just the trailer.”

In a statement, the group said six militants, including three suicide bombers, carried out the assault. After a gun battle that lasted nearly nine hours, Pakistani police officials said a total of seven militants had been killed.

An army spokesman, Maj. Gen. Asim Bajwa, said the attackers sought “to inflict maximum harm” and took no hostages. Hundreds of people were also wounded as classrooms erupted in chaos and carnage, with students and teachers shot point-blank.

The school, while open to the public, is funded by Pakistan’s army, and many students are children of military personnel based in Peshawar.

“My son was in uniform in the morning. He is in a casket now,” wailed one father, Tahir Ali, as he collected the body of his 14-year-old son, Abdullah, according to the Associated Press. “My son was my dream. My dream has been killed.”

Pervaiz Khattak, chief minister of Khyber Pakhtunkhwa province, said the Taliban attackers started “indiscriminate firing” after entering the school through a back door. The first students targeted were gathered in the auditorium to receive first-aid training, police said.

Muhammad Harris, 16, said he was in a room with 30 students and four teachers when they heard a commotion in the hall. The students said some of the attackers appeared to be speaking Arabic.

“Our female teacher went outside when we heard the firing and was shot dead,” Harris said. “One attacker was crying, ‘Help me, I am injured.’ But he was not and was trying to trap us and shoot us.”

Dozens of relatives, desperate for information about missing students, tried to reach the school on foot but were pushed back by a cordon of military guards as emergency and security vehicles rushed by. Some relatives shouted angrily; others milled about in distress.

One man looking for his nephew, an eighth-grader named Walid, said he had searched through the emergency wards and the morgue at Lady Reading Hospital, where many victims of the attack were taken.

“I saw all of the patients and all of the dead,” said Hameed Mohammed, 38. “There was no sign of him.”

As darkness fell, families were still waiting at the roadblock and the military school compound was shrouded in fog. From a distance, men with flashlights could be seen, searching slowly from room to room.