WASHINGTON — A federal appeals court on Friday threw out lengthy prison sentences for three former Blackwater Worldwide security contractors and ordered a new trial for a fourth involved in a deadly 2007 shooting in Baghdad that became a symbol of unchecked, freewheeling American power in Iraq.

The shooting killed or injured at least 31 civilians when contractors unleashed a torrent of machine-gun fire and launched grenades into a crowded downtown Baghdad traffic circle from their heavily armored trucks. An F.B.I. agent once called it the “My Lai massacre of Iraq.”

The ruling is a setback to the effort — which now stretches across three presidential administrations — to demand stiff consequences for 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, it was among the war’s darkest moments and stained the reputation of the United States.

Three of the contractors — 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, Nicholas A. Slatten, a sniper who the government said fired the first shots, was convicted of murder and received a life sentence.