Pakistan's military says its commandos have freed 22 hostages held by militants inside the army's headquarters. Three captives and four militants were killed in the early morning raid.

Army spokesman Maj. Gen. Athar Abbas says "mopping operations" inside the building were on-going early on Sunday.

As the operation began, two explosions and scattered gunshots could be heard from inside the building, which is located in the heavily fortified city of Rawalpindi, not far from the capital city of Islamabad.

Earlier, armed gunmen disguised in military uniforms stormed the headquarters and held a number of soldiers inside the compound, officials said.

While four of the militants were killed in that attack, four or five others managed to infiltrate the compound, officials said.

Authorities had originally denied reports that any gunmen had made it inside the building. But later Saturday, a military spokesperson confirmed that the militants had yet to be captured.

There had been speculation that army chief Gen. Ashfaq Parvez Kayani was inside the complex at the time of the attack. He had been scheduled to attend meetings there and at the president's office, according to military statements.

But Abbas said late Saturday that no senior military or intelligence officials are among the hostages.

The attack began shortly after the noon hour local time when the gunmen, wearing army fatigues, pulled up to the compound's main gate in a white van and opened fire on security personnel, according to Abbas.

The gunmen then left the van and carried on to a second checkpoint before spreading out, Abbas said. They set off at least one grenade and fired a number of rounds at security personnel, who were likely caught off guard by the militants' disguises.

A 45-minute gunfight left four militants dead.

Six military personnel were killed, including a brigadier and a lieutenant colonel, according to an official who spoke on condition of anonymity.

Five other troops were wounded, one critically, Abbas said.

CNN reporter Reza Sayah said the brazen nature of the attack, and the fact the gunmen were wearing disguises, shows "more determination and more sophistication on the part of the militants."

"This is a heavily secured area, (with) check posts at three separate gates," Sayah told Â鶹´«Ã½ Channel Saturday in an interview from Pakistan. "But the dilemma when facing these types of insurgents, militants who are willing to die, militants who welcome death, it's very difficult to stop them without them inflicting some sort of damage. And that's what you saw today."

Khan Bahadur, a shuttle van driver who was standing outside the compound when the attack began, said the gunfight sparked mass chaos at the first checkpoint.

"There was fierce firing, and then there was a blast. Soldiers were running here and there," Bahadur said. "The firing continued for about a half-hour. There was smoke everywhere. Then there was a break, and then firing again."

Pakistani media reported that the Taliban had claimed responsibility for the attack. Meanwhile, two news channels known for being critical of the Pakistani government, Geo and SAMA, went off the air late Saturday.

The attack was the third major assault by militants in Pakistan in the past week.

On Monday, a bombing at the headquarters of a UN aid agency in Islamabad killed five people. In that attack, the bomber was also wearing a security forces' uniform and was allowed entry into the building when he asked to use the bathroom.

On Friday, a suicide bomber blew up a car near a crowded outdoor market in the northwestern city of Peshawar, killing 49 people.

That attack led to promises by the Pakistani government to launch a new series of offensives against militant strongholds along the Afghan border.

The United States has been pushing Pakistan to take strong action against insurgents who are using its soil as a base for attacks in neighbouring Afghanistan. A push into the rugged mountains of South Waziristan could be risky for the army, which was beaten back on three previous offensives into the Taliban heartland there and forced to sign peace deals.

But the army may have been emboldened by a reasonably successful military campaign in the Swat Valley and adjoining Buner district and by the killing in a U.S. missile strike of Taliban leader Baitullah Mehsud. The military also appears committed to destroying Mehsud's group, as opposed to its often ambivalent position toward other insurgents in the past.

With files from The Associated Press