CHATTANOOGA, Tenn. — Marines and sailors risked their lives for one another in Chattanooga last week, trying to distract the gunman who assaulted a naval center, helping people scale a fence for safety and returning fire at the attacker, law enforcement officials said Wednesday.

Some of the five servicemen who were fatally wounded effectively sacrificed themselves during the assault on Thursday, diverting the gunman from a larger group of potential victims, according to a law enforcement official briefed on the investigation into the killings.

“This could have been a lot worse,” said the official, who did not want to be identified because he was not authorized to discuss the investigation. “It could have been a horrible, horrible massacre — so much worse.”

At a news conference here, the F.B.I. confirmed that at least one service member shot at the attacker, but did not say whether he had managed to wound the gunman, Mohammod Abdulazeez, who was killed minutes later in a shootout with the Chattanooga police.