Pentagon chief Chuck Hagel on Wednesday staunchly defended the swap of five Taliban detainees for a US soldier as a "tough call" but a necessary one to secure Sergeant Bowe Bergdahl's release.

Seeking to counter a barrage of criticism from lawmakers, Hagel insisted President Barack Obama had to act quickly given Bergdahl's deteriorating health and that the swap deal brokered by Qatar represented the "last, best opportunity" to ensure the soldier's freedom.

"We made the right decision, and we did it for the right reasons — to bring home one of our own people," Hagel told a tense hearing before the House Armed Services Committee.

Hagel, the first administration official to testify publicly about the swap, said Obama faced a "tough call" but made the right choice.

He described a dramatic chain of events leading up to Bergdahl's release, with US officials worried about Taliban militants staging an attack on special operations forces receiving the American soldier.

After signing a memorandum with Qatar on May 12 on the details of the transfer of the Taliban detainees, the Qataris issued a warning to US officials that "time was not on our side," Hagel said.

"This indicated that the risks to Sergeant Bergdahl's safety were growing," he said.

Up to one hour before the release, the United States did not know the precise location where Bergdahl would be handed over, he said.

– 'Dangerous precedent' –

The May 31 exchange was in keeping with past US conflicts and there was no prospect of prosecuting the Taliban detainees held at the US prison in Guantanamo Bay, Cuba, Hagel said.

But Republicans at the hearing hammered away at Hagel, often interrupting him, accusing the White House of making concessions to "terrorists" and violating its legal obligation to consult with Congress.

"This transfer sets a dangerous precedent in negotiating with terrorists," said Buck McKeon, chairman of the House Armed Services Committee.

"It reverses longstanding US policy and could incentivize other terrorist organizations, including Al-Qaeda, to increase their use of kidnappings of US personnel," he said.

The exchange for Bergdahl has turned into a growing political problem for the White House.

A new ABC News/Washington Post poll showed a majority of Americans opposed the deal, with 53 percent saying they disapproved. If Bergdahl is shown to have deserted, then 63 percent rejected the swap.

McKeon and others charged the Obama administration had failed to abide by a law requiring 30 days' notice to lawmakers before detainees are transferred out of the Guantanamo prison.

In a testy exchange with raised voices, Mike Conaway, a Republican from Texas, told Hagel: "Your actions say you don't trust Congress."

Hagel fired back: "I never said I don't trust Congress."

Some Democrats in Congress also criticized the White House for not keeping lawmakers abreast of the situation, and Hagel acknowledged that the administration may have fallen short on that count.

"We could have done a better job of keeping you informed," said Hagel.

But Hagel, a former Republican senator from Nebraska who broke ranks with his party over the Iraq war, added it was an "extraordinary situation" that could have collapsed if word had leaked of the plan.

The administration had "a fleeting opportunity" and there were few options available, he said.

Invoking his own service as an army sergeant in Vietnam, Hagel said wars were "messy" and presented "imperfect choices."

"War is a dirty business. And we don't like to deal with those realities, but realities they are. And we must deal with them," he said.

The Pentagon's top lawyer, Stephen Preston, told the same hearing that at some point, the United States would no longer be at war with the Taliban, and then it would no longer be possible to hold the insurgents at Guantanamo under "international law."