They were the first known US military fatalities in Pakistan's lawless tribal regions near the Afghan border and a major victory for militants who have been hit hard by a surge of US missile strikes and a major Pakistani army offensive.
The blast also killed three schoolgirls and a Pakistani soldier who was traveling with the Americans. Two more US soldiers were wounded, along with more than 100 other people, mostly students at the school, officials said.
The attack took place in Lower Dir, which like much of the northwest is home to pockets of militants. The Pakistani army launched a major operation in Lower Dir and the nearby Swat Valley last year that succeeded in pushing the insurgents out, but isolated attacks have continued.
The Americans were travelling with Pakistani security officers in a five-car convoy that was hit by a bomb close to the Koto Girls High School.
"It was a very huge explosion that shattered my windows, filled my house with smoke and dust and also some human flesh fell in my yard," said Akber Khan, who lives some 45m from the blast site.
The explosion flattened much of the school, leaving books, bags and pens strewn in the rubble.
"It was a horrible situation," said Mohammad Siddiq, a 40-year-old guard at the school. "Many girls were wounded, crying for help and were trapped in the debris."
Siddiq said the death toll would have been much worse if the blast had occurred only minutes later because most of the girls were still playing in the yard and had not yet returned to classrooms, some of which collapsed.
"What was the fault of these innocent students?" said Mohammed Dawood, a resident who helped police dig the injured from the debris.
The soldiers were part of a small contingent of American soldiers training members of the paramilitary Frontier Corps, Pakistan's army and the US Embassy said. The mission is trying to strengthen the ill-equipped and poorly trained outfit's ability to fight militants.
The soldiers were driving to attend the inauguration of a different girl's school, which had been renovated with US humanitarian assistance, the embassy said in a statement. The school that was ravaged by the blast was not the one where the convoy was heading, security officials said.
US special envoy to Pakistan, Richard Holbrooke, said it did not appear that the attack directly targetted the Americans.
But the blast, which police said was detonated by remote control, hit the vehicle in which the Americans were traveling along with members of the Frontier Corps, according to Amjad Ali Shah, a local journalist traveling with the convoy to cover the school opening.
Holbrooke also said the US has not tried to hide its training mission with the Pakistani military.
"There is nothing secret about their presence there," he told reporters in Washington.
Still, the attack will highlight the existence of US troops in Pakistan at a time when anti-American sentiment is running high. US and Pakistani authorities rarely talk about the American training programme in the northwest out of fear it could generate a backlash.
Despite the presence of tens of thousands of US forces in neighboring Afghanistan, Pakistan does not permit American troops to conduct military operations on its soil.
In a statement, the US Embassy said three American military personnel were killed and two were wounded in the bombing. The Pakistani government condemned the attack in a statement that referred to the dead Americans only as US nationals.
The last American killed in an attack in Pakistan was an American aid worker in the northwestern city of Peshawar in 2008.
Two Pakistani reporters traveling in the same convoy as the Americans said that Pakistani military guides referred to the foreigners traveling with them as journalists. Initial reports of the attack, which proved incorrect, said four foreign journalists had been killed.
Mohammad Israr Khan, who works for Khyber TV, said two of the foreigners were wearing civilian clothes, not uniforms or traditional Pakistani dress.
"When our convoy reached near a school in Shahi Koto, I heard a blast," Shah, the journalist said. "Our driver lost control and something hit me and I fell unconscious."
The Frontier Corps training programme was never officially announced, a sign of the sensitivity for the Pakistani government of allowing US troops on its territory. It began in 2008.
Frontier Corps officials have said the course includes classroom and field sessions. US officials have said that the programme is a "train-the-trainer" programme and that the Americans are not carrying out operations.
After the bombing, the bodies of three foreigners and two injured were flown by helicopter to Islamabad and then taken to the city's Al-Shifa hospital, said a doctor there who asked his name not be used citing the sensitivity of the case.
One of the injured had minor head wounds and the other had multiple fractures. The injured were later taken to a Pakistani military air base and flown out of the country, the doctor said.