Three hostages were also killed in firing by terrorists while two commandos of the elite Special Service group died during the rescue mission, chief military spokesman Maj Gen Athar Abbas said.
Five security personnel were also injured. A fifth terrorist, identified as Aqeel alias Dr Usman and believed to be the mastermind behind the attack that began on Saturday, was captured in an injured condition, said.
Aqeel is also believed to be behind the attack on Sri Lankan cricket team in Lahore [ Images ] on March 3 and failed attempts on former President Pervez Musharraf's [ Images ] life. Four terrorists and six soldiers, including a brigadier from Military Intelligence and a lieutenant colonel, were killed during Saturday's fierce gun battle.
In all, at least 19 people were killed during the attack -- 6 soldiers, 2 commandos, 8 terrorists and 3 hostages. At least two of the terrorists killed on Sunday morning were wearing suicide jackets and intended to blow themselves up in the event of an assault by the military, Abbas said.
One of them had positioned himself in a room where 22 hostages were being held. In all, 42 hostages, including security personnel and civilian employees of the military, were freed
The commandos launched the final assault to free the hostages a few minutes before 6 am local time. Gunshots and several explosions were heard as helicopters hovered overhead.
Abbas said the operation was completed at 9.30 am. Troops were clearing the area and collecting evidence, he added.
"Security forces have complete control of the building," Abbas told mediapersons. It is believed that the building in which the terrorists had barricaded themselves was a key office of the Military Intelligence agency.
The operation to free the hostages was launched nearly 20 hours after terrorists dressed in military uniforms tried to storm the army's heavily fortified General Headquarters in Rawalpindi near Islamabad [ Images ] on Saturday.
The terrorists, who were in a white van, opened fire when they were challenged at a check post near a stadium outside the military compound.
They then left the van and advanced towards a second check post, firing with automatic weapons and lobbing grenades. The terrorists then holed up in a building near the second check post and took security personnel and civilian employees hostage.
The army had initially said the attackers were holding only 10 to 15 people hostage but later reports suggested that they captured over 50 people. Eight hostages were reportedly freed late on Saturday night.
The military made elaborate preparations for the rescue mission on Sunday morning to minimise collateral damage, Abbas said. Electricity in the area where the terrorists were hiding was cut off at about 9 pm on Saturday night.
Pakistan Interior Minister Rehman Malik [ Images ] told a TV channel that security forces gathered vital clues about the terrorists after raiding a house on the outskirts where they had lived for the past two months.
"During the raid, fuses of the type used in suicide jackets were found. So we knew the terrorists would have suicide jackets and we had to be careful," he said.
Military officials said nine officers and 22 soldiers were among the hostages freed on Sunday. The rest were civilian employees of the military. Suicide jackets, grenades, explosives and improvised explosive devices were recovered by troops from the dead terrorists.
A faction of the banned Tehrik-e-Taliban Pakistan claimed responsibility for the attack but Interior Minister Malik said the Taliban were only a front for the Al Qaeda [ Images ]. He said most recent terror attacks were launched jointly by the Taliban, Al Qaeda and other banned groups.
These attacks also had their roots in the lawless South Waziristan tribal region and the government had decided to launch a military operation against militants in the area, Malik said.
The government had given the mandate for the operation to army chief Gen Ashfaq Parvez Kayani [ Images ] and the military's top leadership will decide the time for launching the campaign, he added.