ISLAMABAD, Pakistan (CNN) -- A total of 39 hostages in Pakistan were freed Sunday morning after being held by five militants at the army headquarters in Rawalpindi, an army spokesman said.

Pakistani soldiers take up position after an attack on the army headquarters in Rawalpindi on Saturday.

Four militants and three hostages -- two of them civilians -- were killed in the 22-hour standoff, Gen. Athar Abbas told CNN. Two security personnel were killed during the rescue operation, and five others were injured.

Abbas said two of the militants -- one wearing a suicide vest -- held 22 of the hostages in one room and threatened to blow up the building. Security forces fatally shot both gunmen before the explosive detonated.

Two other militants, who were in another part of the facility, blew themselves up, Abbas said. Whether the three hostages died in during those explosions was unclear.

A fifth militant -- who led the attack -- was injured and captured, Abbas said. That militant was identified as Aqil, aka Dr. Usman, one of the suspected masterminds in the March attack on the Sri Lankan cricket team in Lahore, Pakistan. Police there also have said he was involved in the July 2007 attempt to attack the airplane of former Pakistani President Pervez Musharraf.

Don't Miss

Dr. Usman was injured when he blew up explosives in the building he was holed up in, Abbas said.

The military continued to revise its initial numbers of the dead and injured and those taken hostage after the standoff ended about 10 a.m. Sunday.

Earlier, Abbas told CNN that the hostages included civilians and military personnel. Military officials said sporadic shootings flared between the gunmen and authorities for several hours.

Abbas said security forces communicated throughout the standoff with the militants, who made demands.

Though Abbas did not immediately elaborate on the nature of the demands, another military official -- who was not authorized to speak on the record -- said the gunmen wanted the government to release several militants in Pakistani custody.

The standoff started after an attack on the army checkpoint, which occurred about noon. The gunmen, wearing camouflage and riding in a minivan, opened fire Saturday at the headquarters checkpoint, Abbas said.

Six army guards were killed initially in the gunbattle, Abbas said, and five gunmen were killed.

Another military official said the Taliban had claimed responsibility for the attack.

Rawalpindi, the closely guarded home to the general headquarters of the Pakistani army, has experienced suicide bombings and other attacks before.

Saturday's assault was the third major attack in Pakistan this week.

A suicide car bomb in the northern city of Peshawar killed at least 52 people Friday, and a suicide attack at the offices of the U.N. World Food Programme in Islamabad on Monday killed five employees.