The Pakistani army was preparing for a major anti-Taliban operation
Pakistani security forces have freed about 40 hostages who were being held by militants at an army base in Rawalpindi, near the capital Islamabad.
Military officials said three hostages and two soldiers died in the operation along with at least four militants, one of whom was wearing an explosives belt.
One insurgent, named by police as Aqeel, was taken alive but wounded. He was identified as the group's leader.
The insurgents had attacked Pakistan's military HQ on Saturday.
Six soldiers and four militants were killed in the initial assault.
"The operation is over. It was highly successful," military spokesman Maj Gen Athar Abbas told Pakistani television.
Aleem Maqbool, BBC News, Islamabad
This has been an audacious attack by militants with a level of coordination and sophistication.
While Pakistan is claiming the rescue operation was a success, it's a huge embarrassment that one of the most secure areas in Pakistan - where top military officials are based - has been attacked.
Many people here are nervous that the body supposed to protect them has been attacked at its very heart.
"The situation is now totally under control. The entire area has been cleared. There are no terrorists inside," he said.
But the BBC's Aleem Maqbool in Islamabad says it is a huge embarrassment for the military that one of the country's most sensitive areas has been attacked.
Meanwhile US Secretary of State Hillary Clinton, speaking in London, said the attack showed the increasing militant threat against the authority of the state.
Interior Minister Rehman Malik said an operation against militants in South Waziristan was now inevitable.
"Now the government has no other option but to launch an offensive," he said.
"What happened in Peshawar, Islamabad and today, all roads lead to South Waziristan."
The sound of blasts and gunfire rang out as Pakistani special forces entered the compound for the pre-dawn raid.
RECENT MILITANT ATTACKS
24 Sept - Seven pro-government tribal elders killed by militants in town of Janikhel, north-western Pakistan
26 Sept - At least 16 people killed in two suicide car bombs, in Peshawar and Bannu
5 Oct - Suicide bomber attacks UN offices in Islamabad killing five
9 Oct - At least 50 killed in suspected suicide bombing in Peshawar
Gen Abbas said the forces had met resistance in what he described as a "very skilled" operation.
They found the hostages being held in a room "with a terrorist who was wearing a suicide jacket", he said.
Gen Abbas said the commandos had "acted promptly" and shot the suspected hostage taker "before he could pull the trigger".
"Three of the hostages were killed due to militant firing," Reuters quoted him as saying.
The military said later that the surviving militant, named as Aqeel or Dr Usman, was the leader of the group and was also suspected of masterminding an attack on the Sri Lankan cricket team in Lahore in March.
Senior military officials and civilian personnel were reported to be among the hostages.
The attack came as the army was preparing for a major operation against the Taliban, and officials said the militant group had claimed responsibility.
The Taliban had been threatening to carry out attacks unless operations against it were stopped.
Witnesses said that the gunmen had driven up to the army compound in a white van just before midday local time (0600 GMT) on Saturday.
They took up positions, fired on the compound and threw hand grenades, security officials said.
The military reported that the attack had been repelled after a gun battle lasting around 45 minutes but later admitted hostages had been taken.
The attack followed a series of bombings in north-western Pakistan. On Friday at least 50 died in a blast in Peshawar.
In recent days Taliban positions in the tribal areas have been bombed by the air force, amid speculation that the army's offensive there is soon to be intensified, says our correspondent.
There was a period of relative quiet in August after Taliban leader Baitullah Mehsud was killed, but the rate of militant attacks has increased since then, he adds.
Islamist militants have carried out a number of attacks against high-profile, high-security targets in recent years.
In March this year gunmen opened fire on a bus carrying the Sri Lankan cricket team in the city of Lahore. Six policemen and a driver were killed and several of the team were injured.
In the same month, dozens of people were killed when a police training centre on the outskirts of the city was occupied by gunmen.