A Pakistan army operation to free hostages taken after an attack on the military headquarters has ended with all militants killed or arrested and at least 30 hostages freed, an army spokesman said.

"The operation is over. It was highly successful. We have rescued the hostages in a such a large number," military spokesman Major General Athar Abbas told state-run television.

"The situation is now totally under control ... the entire area has been cleared. There are no terrorists inside."

He says three hostages, two commandos and four of the gunmen were killed. Two commandos were wounded and one wounded militant was arrested.

On Saturday (local time), five militants barricaded themselves into an office just inside the military headquarters in the garrison city Rawalpindi, which adjoins the capital Islamabad, Major General Abbas said.

The drama unfolded just before midday on Saturday, when nine Taliban gunmen in military uniform and armed with automatic weapons and grenades drove up to the military compound and shot their way through one checkpost.

Four militants and six soldiers were killed near a second post but the rest of the rebels fled during the fight, taking military employees hostage in the third dramatic strike blamed on Taliban militants in the past week.

The attack on the tightly guarded headquarters came as the military prepared a major offensive against the militants in their north-western stronghold of South Waziristan on the Afghan border.

It calls into question recent government claims setbacks for the militants this year had put them on the defensive, but a senior official says it will not hinder plans to tackle the South Waziristan bases.

The strike at the heart of the powerful military may also 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.

International condemnation

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 Australian Government has also condemned the attack, with Foreign Affairs Minister Stephen Smith offering Australia's condolences.

"This very much underlines the gravity of the threat that Pakistan faces," he said.

"It very much reinforces Australia's desire to be of assistance to Pakistan at this time."

- AFP/Reuters/ABC