None of the hostages were hurt. But the standoff underscored the government's fragile grip on Pakistan's borderlands near Afghanistan, where crime is rife and security forces are struggling to contain rising Islamic militancy.
Kidnapping for ransom is common in Pakistan, particularly in the northwest, and police said the gunmen were criminals seeking profit rather than militants.
The mounting violence has contributed to the growing unpopularity of President Pervez Musharraf, who was on his last stop Monday of a European tour. After talks with British Prime Minister Gordon Brown, Musharraf insisted his U.S.-backed policies to fight religious extremism were working.
"I think we are succeeding," he told a news conference.
He also played down the kidnapping.
"It was incidental that those criminals entered the school," the president said. "It has been resolved peacefully."
Police said the half-dozen gunmen seized control of the school near the town of Bannu after a botched attempt to kidnap the government health chief from a neighboring district.
Police had given chase, sparking a firefight in which one gunman died and a policeman was wounded. The health official and two relatives abducted with him were freed.
The gunmen took refuge inside the school and threatened to kill the children, teachers and themselves if anyone attacked. So tribal elders started negotiations while armed villagers and security forces stood guard outside, said former lawmaker Shah Abdul Aziz, one of the negotiators.
In return for releasing the captives and giving up their weapons, the gunmen were given safe passage and left for an unknown destination, he said.
Local police and the government declined to comment on that report.
There was conflicting information on the number of children and teachers who had been held hostage. Aziz said there were 315 children, aged between 8 and 11, and 10 teachers. But local police chief Hamza Mehsud said there had been only 25 children and seven teachers.
Pakistan's government has struggled to control the tribal regions along the border with Afghanistan. A series of peace deals Pakistan struck in 2005 and 2006, under which tribal leaders were supposed to police the lawless area in return for a withdrawal of troops, quickly fell apart.
U.S. officials criticized the deals, saying they allowed al Qaeda to regroup and provided a secure rear base for resurgent Taliban fighters in Afghanistan. Osama bin Laden and his top deputy, Ayman al-Zawahri, are also believed to be hiding in the border zone.
Meanwhile Monday, about 50 miles southwest of Bannu, security forces clashed with militants in the tribal region of South Waziristan, where Taliban and al Qaeda fighters find sanctuary. Violence there has left hundreds dead in this month alone.
Army spokesman Maj. Gen. Athar Abbas said two Frontier Corps troops were killed and five or six wounded in fighting in the areas of Makin and Kotkai on Monday.
A local intelligence official, who declined to be named because he was not authorized to speak to journalists, said 18 bodies of Taliban fighters lay in the battlefield area. Abbas gave no figure for militant casualties.
About 800 local residents fled to North Waziristan to escape the fighting, and others moved to the towns of Tank and Dera Ismail Khan, said Fazal Subhan, a tribal elder.
Associated Press writers Bashirullah Khan in Miran Shah and Zarar Khan in Islamabad contributed to this report.