* Thirty-nine hostages freed, three killed
* Military says ringleader captured
* U.S., UK confident of nuclear security
* Waziristan offensive imminent
(Adds interior minister)
By Augustine Anthony
RAWALPINDI, Pakistan, Oct 11 Pakistani commandos stormed an office building on Sunday and rescued 39 people taken hostage by suspected Taliban militants after a brazen attack on the army's headquarters.
Saturday's attack on the tightly guarded army headquarters in the city of Rawalpindi, next door to the capital, Islamabad, came as the military prepared an offensive against the militants in their stronghold of South Waziristan on the Afghan border.
The strike at the heart of the powerful military called into question government assertions the militants were virtually crippled by recent setbacks. But officials said it only underlined the need to finish them off.
"It has been decided, the civilian leadership has decided ... the operation is imminent," Interior Minister Rehman Malik told Reuters in an interview in Singapore.
Three hostages, two commandos and four of the gunmen were killed in the pre-dawn rescue operation, said army spokesman Major General Athar Abbas. One wounded gunman was captured and Abbas said he was the ringleader.
"Now there is no terrorist left there. The operation is over," Abbas told Reuters.
The United States condemned the latest attack and expressed confidence in the security of its ally's nuclear arsenal.
Pakistani Taliban militants linked to al Qaeda have launched numerous attacks over the past couple of years, most aimed at the government and security forces, including bomb attacks in Rawalpindi.
On Saturday, gunmen wearing army uniforms attacked the army headquarters, killing six soldiers including a brigadier and a lieutenant colonel in a gunbattle at a main gate.
Five gunmen were killed there and two of their wounded colleagues captured. But others fled and took hostages in a building housing security offices near the headquarters.
Commandos launched their assault under cover of darkness with a blast and gunfire erupting at 6 a.m. (midnight Saturday GMT).
"They were in a room with a terrorist who was wearing a suicide jacket but the commandos acted promptly and gunned him down before he could pull the trigger," Abbas said of one large group of hostages.
"Three of the hostages were killed due to militant firing," he said. More hostages were later found alive.
Interior Minister Malik said the raid, which bore the hallmarks of several similarly ruthless "swarm" attacks this year, was apparently carried out by Pakistani Taliban and al Qaeda.
In March, gunmen attacked Sri Lanka's cricket team as it drove to a match in the city of Lahore and weeks later militants raided a police cadet college in the same city.
Those attacks were blamed on the Pakistani Taliban, widely believed to have been helped by militants from Punjab province.
Abbas identified the militant captured on Sunday as Aqeel, alias Dr Usman. A security official in Punjab said Usman was believed to be a member of the Lashkar-e-Jhangvi group.
"Militants we arrested in Lahore had told us during interrogation that he masterminded the attack on the Sri Lankan team and provided weapons," said the official.
Some hostage takers' phone calls were intercepted and they were speaking Punjabi, another security official said.
The attack on the army came at the end of a violent week.
Last Monday, a suicide bomber attacked a U.N. office in Islamabad killing five staff members, and on Friday a suspected suicide bomber killed 49 people in Peshawar.
Malik said the planned offensive against the militants in South Waziristan was not a matter of choice, but a necessity.
"It is not an issue of commitment, it is becoming a compulsion because there was an appeal from the local tribes that we should do the operation."
Washington needs Pakistani help against militants crossing into Afghanistan to fight U.S.-led forces there and has been urging action against Afghan Taliban factions on the border.
In March, militants pushed to within 100 km (60 miles) of Islamabad, sparking grave concern among allies, including the United States, for Pakistan's prospects, and fears for the safety of its nuclear weapons.
In late April, the army launched an offensive in Swat, northwest of Islamabad, and largely cleared out the Taliban.
The militants suffered another big blow on Aug. 5, when their leader, Baitullah Mehsud, was killed in a missile attack by a U.S. drone aircraft. His successor vowed revenge last week.
U.S. Secretary of State Hillary Clinton condemned the Saturday attack and she and British Foreign Secretary David Miliband expressed confidence in the security of Pakistani nuclear facilities.
Washington had every confidence in the government's control over its nuclear weapons, Clinton told a London news conference. (Reporting by Kamran Haider and Zeeshan Haider in ISLAMABAD, Keith Weir and Jeff Mason in LONDON; Saeed Azhar and Sanjeev Miglani in Singapore; Writing by Robert Birsel; Editing by Myra MacDonald)