One attacker, described as the militants' ringleader, was captured.
The attack on Pakistan's "Pentagon," home to the nation's most powerful institution, showed the continued strength of insurgents allied with al-Qaida and the Taliban despite military operations and U.S. missile strikes that have battered their ranks. It was the third major attack in Pakistan in a week and threatens to deflate the army's growing popularity in the wake of successful operations against the Taliban in the Swat Valley, Buner and Bajur.
The government said the siege only steeled its resolve to go through with an offensive in South Waziristan, a tribal region along the Afghan border and a major militant stronghold. The U.S. and Pakistan's other Western allies want Islamabad to take more action against insurgents also blamed for soaring attacks on U.S. and NATO troops in Afghanistan.
A leading analyst said the militants' ability to invade the heavily guarded army headquarters, even securing uniforms, was evidence they may have infiltrated the security forces. At the very least, he said, it shows the army is constantly forced to play defense.
"The question is, when do they get ahead of the curve where they can actually be in preventative mode?" said Kamran Bokhari, an analyst with Stratfor, a U.S.-based global intelligence firm.
Five heavily armed militants took the hostages after they and about four other assailants attacked the headquarters' main gate Saturday, killing six soldiers, including a brigadier and a lieutenant colonel. The gunmen arrived in a white van that reportedly had army license plates.
No group claimed responsibility, but authorities said they were sure the Pakistani Taliban or an allied Islamist militant group were behind the strike.
The garrison city of Rawalpindi, just a few miles (kilometers) from Islamabad, is filled with security checkpoints and police roadblocks.
Explosions and gunshots rang out just before dawn Sunday as commandos moved into a building in the complex, while a helicopter hovered in the sky. Three ambulances were seen driving out of the heavily fortified base close to the capital, Islamabad.
Two hours after the raid to retake the compound began, two new explosions were heard. The army said it was "mopping up" the remaining insurgents.
The hostages included soldiers and civilians. Army spokesman Maj. Gen. Athar Abbas said 20 hostages were kept in a room guarded by a militant wearing a suicide vest who was shot and killed before he managed to detonate his explosives.
Overall, 20 people died - six soldiers, two commandos, nine militant attackers and three captives - and several were wounded. The final hostage-taker was caught as he wounded himself by setting off explosives he was carrying, Abbas said.
Abbas identified the captured man as Aqeel, alias "Dr. Usman," and described him as "the leader of all this group." The name matched that of a militant suspected of orchestrating an attack in Lahore earlier this year on Sri Lanka's visiting cricket team.
The weekend siege followed a car bombing that killed 53 on Friday in the northwestern city of Peshawar and the bombing of a U.N. aid agency Monday that killed five in Islamabad. The string of attacks destroyed any remaining hope that the militants had been debilitated by the death of Pakistani Taliban chief Baitullah Mehsud in a U.S. missile strike in August.
U.S. Secretary of State Hillary Rodham Clinton told reporters in London that the weekend siege showed that militants are "increasingly threatening the authority of the (Pakistani) state, but we see no evidence they are going to take over the state."
She and British Foreign Secretary David Miliband both said, however, that there was no sign Pakistan's nuclear arsenal was at risk.
A police intelligence report obtained by The Associated Press on Saturday had warned in July that members of the Taliban along with Jaish-e-Mohammed, a militant group based in the country's Punjab province, were planning to attack army headquarters after disguising themselves as soldiers. The report was given to the AP by an official in the home affairs ministry in Punjab's home department.
A week ago, Baitullah Mehsud's successor, Hakimullah Mehsud, told journalists summoned to a briefing in South Waziristan that the Taliban would launch more attacks on military, government and other targets in the country.
Officials said Saturday that they had raided a house in the capital where the attackers were believed to have stayed. They found military uniforms and bomb-making equipment.
The army - which until 2001 had supported various militant groups for use as proxies in Afghanistan and India - had previously been unwilling to go into Waziristan. Three earlier offensives there have ended in failure, and no one thinks the fight against an estimated 10,000 well-armed fighters there will be any easier this time.
Interior Minister Rehman Malik said a Waziristan offensive was now "inevitable."
"We are going to come heavy on you," he warned the militants.
Bokhari, the analyst, said the plans for the latest offensive appeared to have prompted the militants to launch a pre-emptive strike.
"It's an attempt to shake the confidence of the government," he said.