A group of insurgents wearing Afghan army uniforms launched a suicide attack and stormed a courthouse Wednesday in a failed bid to free Taliban inmates, killing at least 44 people, half of them shot in the basement.

The attack — one of the deadliest in the more than 11-year-old war — began about 8:30 a.m. when about nine men wearing suicide vests drove into the capital of Farah province in western Afghanistan, evading checkpoints by using army vehicles, according to the provincial police chief. The standoff ended some eight hours later when the last gunman was killed.

Insurgents have stepped up assaults targeting the Farah provincial government in recent months as they vie for control of an area bordering Iran to the west and Helmand province to the east. Farah has become increasingly volatile as the site of a growing drug trade after military offensives in neighboring areas.

“The Taliban seem to be exploiting the opium harvest and the unpopular eradication efforts by the government to further establish their presence,” Fabrizio Foschini, of the independent research group Afghan Analysts Network, said in a recent blog.

Militants have staged high-profile complex attacks across Afghanistan in a bid to show their strength and undermine confidence in the central government.

Wednesday’s assault was among the most brutal for civilians, raising fears of deteriorating security as international combat forces withdraw by the end of 2014 and hand over control to Afghan security forces.

The attack began when two assailants blew themselves up inside one of the vehicles while the others jumped out and ran toward the courthouse and prosecutor’s office, provincial police chief Agha Noor Kemtoz said. Guards opened fire, killing one of the attackers, as the others engaged in a fierce gun battle that left civil servants and government officials holed up in their offices.

Other civilians fled to the basement of the courthouse, where gunmen found them and killed 21 people, officials said.

Kemtoz said the attack aimed to free more than a dozen Taliban prisoners who were being transferred to the courthouse for trial, which had not yet started.

“Definitely the plan was to free the prisoners with this attack, but thank God, they did not succeed,” he said. “All the prisoners are accounted for.”

Deputy provincial governor Yonus Rasouli, however, said one of the inmates had escaped. He said the suspects had been arrested in different places and faced a range of charges, including planting roadside bombs.

Provincial Gov. Akram Akhpewak said those killed included 34 civilians, including judges and prosecutors, 10 members of the security forces and the nine attackers. Dr. Abdul Hakim Rasouli, chief of the Farah hospital, said 80 people also were wounded.

Taliban spokesman Qari Yousef Ahmadi claimed responsibility for the attack in a text message sent to reporters, although some witnesses questioned whether the assailants were Afghans.

Provincial lawmaker Humaira Ayobi said a recent police operation targeting the drug trade may have been a factor in the audacious attack.

It was the deadliest strike since Oct. 26, when a suicide bomber detonated explosives outside a mosque packed with senior regional officials in the northern Afghan province of Faryab, killing 41 people on a major Muslim holiday.

Meanwhile, Afghanistan’s intelligence chief Asadullah Khalid returned to Kabul on Wednesday nearly four months after he was seriously wounded by a Taliban suicide bomber posing as a messenger of peace.