WASHINGTON — A federal appeals court on Friday threw out the lengthy sentences for three former Blackwater Worldwide security contractors and ordered a new trial for a fourth man involved in a deadly 2007 shooting in Baghdad.

The shooting injured or killed at least 31 civilians and made Blackwater a symbol of unchecked, freewheeling American power in Iraq.

Firing from heavily armored trucks, the contractors unleashed a torrent of machine gun fire and launched grenades into a crowded traffic circle. An F.B.I. agent once called it the “My Lai massacre of Iraq.”

Three men, Dustin L. Heard, Evan S. Liberty and Paul A. Slough, were convicted in 2014 of voluntary manslaughter and using a machine gun to carry out a violent crime. They were sentenced to 30 years in prison, a mandatory sentence on the machine-gun charge.

A fourth man, Nicholas A. Slatten, a sniper who the government said fired the first shots, was convicted of murder and received a life sentence.

Defense lawyers argued that the men were under fire from insurgents, a claim that prosecutors denied.

The machine-gun charge was always contentious, even inside the Justice Department, where some prosecutors believed it was unfair to add an extra penalty for using a weapon that the United States government required them to carry.

A federal appeals court agreed, saying the machine-gun law was intended to punish people who intentionally brought dangerous weapons with them to carry out violent crimes. The three-judge panel declared the contractors’ sentences “grossly disproportionate to their culpability for using government-issued weapons in a war zone.” The court ordered the three men to be resentenced, a ruling that could significantly reduce their prison terms.

Mr. Slatten’s conviction was thrown out entirely. The appeals court ruled that he never should have been prosecuted in the same trial as his colleagues, one of whom said he — and not Mr. Slatten — fired the first shots.

By overturning his conviction, the court has forced the Justice Department to decide whether to prosecute again. William Miller, a spokesman for the United States attorney’s Office in Washington, said prosecutors were reviewing the opinion and had no additional comment.

Mr. Slatten had faced manslaughter charges, but prosecutors missed a filing deadline in his case and inadvertently let the statute of limitations expire, leaving them with the choice of prosecuting him for murder or dropping the charges.

Retrying Mr. Slatten will not be easy. Prosecutors tracked down dozens of Iraqi witnesses and flew them to Washington for the first trial and would probably have to do so again.

At their sentencing, the contractors predicted they would ultimately be exonerated. “The verdict is wrong,” Mr. Slatten told the judge. “You know I am innocent, sir.”

The court ruling is a setback in an effort that has now stretched across three presidential administrations.

After the shooting, the Bush administration implored an angry and skeptical Iraqi government and its citizens to trust the American criminal justice system. But the case suffered a series of setbacks, some of the government’s own making. Ultimately, the Obama administration won convictions. It will be up to the Trump administration to decide how to proceed.

The shooting, in Baghdad’s Nisour Square, along with the massacre by Marines of 24 Iraqi civilians at Haditha and the abuses of Iraqi prisoners at Abu Ghraib prison, was among the war’s darkest moments and stained the nation’s reputation. The Nisour Square shooting also forced a reconsideration of America’s reliance on contractors in war zones.

During the post-Sept. 11 conflicts, no security contractor was more powerful than Blackwater. Its employees protected American diplomats overseas and worked alongside C.I.A. officers in clandestine counterterrorism operations. The company won more than $1 billion in contracts. Its founder, Erik Prince, ultimately sold the company, which has had several name changes and reorganizations since then and exists now as Academi, a subsidiary of the Constellis Group.

The four former Blackwater contractors had asked the court to overturn the convictions entirely, arguing that the Justice Department had no jurisdiction to bring charges for possible crimes committed in Iraq.

Federal law gives the Justice Department the ability to bring charges against contractors traveling with or supporting the mission of the Defense Department. Lawyers for the defendants argued that, as State Department contractors, the Blackwater guards were not covered by that law. The court disagreed.