Pakistani commandos stormed a building near army headquarters on Sunday and freed 25 hostages being held there by suspected Taliban militants, a military spokesman said.
Three hostages and four of the gunmen were killed, said the spokesman, Major General Athar Abbas.
Saturday's brazen attack on the tightly guarded headquarters in the garrison city of Rawalpindi came as the military prepared a major offensive against the militants in their northwestern stronghold of South Waziristan on the Afghan border.
The strike at the heart of the powerful military is likely to revive fears for nuclear-armed Pakistan's stability at a time when the United States needs its help in the campaign against an intensifying insurgency in Afghanistan.
A blast and gunfire erupted before dawn as soldiers assaulted the security office building near the army headquarters where the gunmen and their hostages were holed up.
"They were in a room with a terrorist who was wearing a suicide jacket but the commandos acted promptly and gunned him down before he could pull the trigger," Abbas said.
"Three of the hostages were killed due to militant firing," he said. Three other hostages were later found alive.
Soldiers were searching for other gunmen, he said, adding there were believed to have been more than five of them in the building.
A Reuters reporter saw three ambulances leaving the area.
On Saturday, gunmen wearing army uniforms attacked the headquarters killing six soldiers in a gun battle at a main gate.
Four gunmen were killed there and two of their wounded colleagues captured, security officials said. But other gunmen fled and took hostages in the office building.
Pakistani Taliban militants linked to al Qaeda have launched numerous attacks in Pakistan over the past couple of years, most aimed at the government and security forces, including bomb attacks in Rawalpindi.
On Monday, a suicide bomber attacked a U.N. office in Islamabad, and on Friday a suspected suicide bomber killed 49 people in Peshawar.
"What happened in Peshawar, Islamabad and today, all roads lead to South Waziristan," Interior Minister Rehman Malik said on Saturday.
"Now the government has no other option but to launch an offensive," he said.
Clinton condemns attack
The raid on the army headquarters bore the hallmarks of several similarly audacious "swarm" attacks this year.
In March, gunmen attacked Sri Lanka's cricket team as it drove to a match in the city of Lahore and weeks later militants raided a police cadet college in the same city.
At around the same time, militants pushed to within 100 km (60 miles) of Islamabad, sparking grave concern among allies, including the United States, for Pakistan's prospects and fears for the safety of its nuclear weapons.
The United States needs Pakistani help against militants crossing into Afghanistan to fight US-led forces there and has been urging action against Afghan Taliban factions on the border.
In late April, the army launched an offensive in the Swat valley, 120 km (80 miles) northwest of Islamabad, and largely clearing the Taliban out.
The militants suffered another major blow on August 5, when their overall leader, Baitullah Mehsud, was killed in a missile attack by a US drone aircraft in South Waziristan.
US Secretary of State Hillary Clinton condemned the Saturday attack saying it showed the threat to the Pakistani government and the very important steps the civilian leadership and military were taking to root out extremists.
The army has not said when it will send ground troops in to South Waziristan.
India has demanded action against anti-India militant groups based in Pakistan's Punjab province and North West Frontier Province Information Minister Iftikhar Hussain called on Saturday for the elimination of militant bases in Punjab.
Even if a South Waziristan offensive was successful militants would get help from Punjab, he told reporters.
Keep up with the latest business news from the region with the Emirates Business 24|7 daily newsletter. To subscribe to the newsletter, please click here.