Soldiers prepare to raid the army headquarters in Rawalpindi, where militants were holding 45 hostages. Photograph: Faisal Mahmood/Reuters

Commandos freed 42 hostages from captivity in a dramatic dawn raid today, ending a 22-hour militant assault on Pakistan's military headquarters that had embarrassed the army as it prepared for an offensive in the Taliban's tribal stronghold.

Nineteen people, including two senior officers and eight militants, were killed during the audacious raid in central Rawalpindi, a garrison city 10 miles from Islamabad.

One militant whom the army identified as the ringleader, Dr Usman, was captured alive and was being treated for his wounds.

Major cities in Pakistan wore a tense air today, as security forces moved into high alert and ordinary Pakistanis digested the news of the brazen raid on the nerve centre of their powerful military machine. It was the third militant spectacular in a week – last Monday, a suicide bomber disguised as a soldier killed five staff in a United Nations building, while on Friday, a huge bomb in central Peshawar killed 53 people.

Analysts said the operation underscored the potency of extremist networks despite a recent defeat by the army in Swat and a stream of US drone attacks in the tribal belt, one of which killed the Taliban leader Baitullah Mehsud in August.

In London, the US secretary of state, Hillary Clinton, said it showed that militants were "increasingly threatening the authority of the state". But, she added, "we see no evidence they are going to take over the state".

The raid also raised questions about the degree of insider co-operation that allowed nine heavily armed attackers to penetrate the country's most sensitive and heavily guarded area.

According to reports, the militants were wearing army uniforms and driving a vehicle bearing military licence plates when they attacked a gate leading to the headquarters, known as GHQ, at 11.30am yesterday.

An intense 45-minute gunfight ensued, in which a brigadier, a lieutenant colonel and several soldiers were killed. Five militants died but another four managed to flee into a nearby security building, where they took about 45 people hostage.

The army sealed off the area and imposed strict media controls, briefly shutting down Geo, the country's most popular television station. Journalists were almost entirely dependent on the army for information about the unfolding drama.

The second stage of the drama came at 6am today, when a barrage of explosions and gunfire rang out across Rawalpindi as Special Services Group (SSG) commandos attacked the besieged building. Three hostages, two of them civilians, and two soldiers were killed.

Four of the remaining five militants were killed, and the army later released blurry photos of their faces. Army spokesman Major General Athar Abbas said 20 of the hostages were kept in a room guarded by a militant wearing a suicide vest, who was shot dead before he could detonate the explosives. But the fifth militant, later identified as the ringleader, Dr Usman, was captured alive after his suicide vest failed to explode. He was taken to hospital and is being treated for his wounds.

Also known as Aqeel, Usman is reportedly a member of Lashkar-i-Jhangvi, a fearsome sectarian group whose members have developed strong ties with al-Qaida in recent years.

A militant of the same name was accused of orchestrating an attack on the Sri Lankan cricket team earlier this year. Some reports described him as a former army medical corps officer.

Analysts said it appeared to be a pre-emptive strike against the army before a much-heralded assault on the Taliban stronghold of South Waziristan, where warplanes have started "softening" up militant targets in recent days.

The interior minister, Rehman Malik, said the Waziristan offensive was now "inevitable". He warned the militants: "We are going to come heavy on you."

Yesterday, military intelligence raided a house where the attackers were believed to have stayed, finding military uniforms and bomb making equipment. This morning, soldiers folded a green and white Pakistani flag over the coffin of the most senior victim of the attack, Brigadier Anwar ul Haq, who was buried in Islamabad.

Sign up for the Guardian Today

Our editors' picks for the day's top news and commentary delivered to your inbox each morning.