BAGHDAD — An Iraqi court has ordered the release of the last detainee handed over to Iraqi custody by the departing United States military, citing insufficient evidence to prosecute him on accusations that he orchestrated a deadly 2007 assault against American forces, his lawyer said Monday.

Although the ruling was automatically appealed, it added fuel to concerns among American officials that transferring the prisoner, Ali Musa Daqduq, to Iraqi authorities in mid-December had cleared the way for his eventual release — a development that would constitute a propaganda coup for Iran and Shiite militias operating in Iraq.

American authorities have said Mr. Daqduq, a Lebanese citizen, had confessed to being a Hezbollah operative who helped to plot a raid in January 2007 by Shiite militants against American forces in Karbala, a Shiite holy city in southern Iraq. The militants, who wore American-style uniforms and carried forged identity cards, killed five American soldiers: one in the raid and four who were captured and whose bodies were later dumped by a road.

The fate of Mr. Daqduq became a vexing political and diplomatic question during the American military’s last days in Iraq. American officials debated whether departing forces should take Mr. Daqduq with them for prosecution. Some Republicans had urged that he be tried before a military commission at Guantánamo Bay, Cuba, where the man accused of being the architect of the Sept. 11 attacks and four co-defendants were arraigned over the weekend.