TALLAHASSEE, Fla. — A furious gun battle erupted inside a federal prison Wednesday when a guard opened fire on FBI agents who had come to arrest him and several others on charges of having sex with female inmates in exchange for money and contraband. Two people were killed and another was wounded.

The dead were the guard and a U.S. Justice Department investigator. A prison employee helping with the arrest was hospitalized.

Six guards in all had been indicted Tuesday on charges they had sex with the women by bribing them with money and contraband in a scheme that went on for two years. The contraband was not specified but could include drugs and alcohol.

When FBI agents and Justice Department investigators arrived at the prison Wednesday to arrest the men, one of the indicted guards shot a federal correctional officer, said FBI spokesman John Girgenti. He said the officers fired back.

The guard fired with a personal weapon, wounding a Bureau of Prisons employee who was assisting with the arrest. Agents from the Justice Department’s inspector general’s office returned fire, killing the guard. A Justice Department agent was killed in the exchange. It was not immediately clear who fired that fatal shot.

“These agents were out just trying to do their job, trying to do an arrest in a very controlled situation, and it just didn’t come down exactly as planned,” FBI agent Michael Folmar said.

Locked downFederal Bureau of Prisons spokeswoman Carla Wilson said the prison had been locked down.

Officials did not release the gunman’s identity, but lawyer Tim Jansen said the dead guard was Ralph Hill, his client. The slain Justice Department agent was identified as William “Buddy” Sentner, 44, who formerly worked for the Secret Service before joining the inspector general’s office in 2002.

The identity of the survivor was not disclosed, but he was reported in stable condition. Folmar said the man should fully recover.

Folmar declined to discuss details until an FBI team set to begin work Thursday completes an investigation into the shooting.

Jansen said Hill’s behavior was “totally out of character” for the Air Force veteran. “He had no criminal history or issues of violence in his background,” Jansen said.

Hill, 43, had voluntarily supplied a saliva sample after authorities began investigating the sex-for-contraband scandal in November, Jansen said.

A woman who answered the front door at Hill’s home declined to comment.

Guards threatened inmatesThe detention center houses mostly men who are being processed before entering the prison system and is part of the Tallahassee Federal Correctional Institution. A low-security prison for female inmates is next to the detention center. Together the men’s and women’s units house 1,445 inmates.

Guards are prohibited from bringing personal weapons into prisons but are not screened the way visitors are, Wilson said.

In addition to the sex charges, the guards were accused of threatening to plant contraband in inmates’ belongings or have them sent to other institutions farther from their families if they reported the illegal activity.

According to the indictment, the guards showed inmates information about themselves and inmates on the prison computer system to prove that their threats were real. It said the guards switched assignments to arrange trysts with inmates.

The five other guards entered not-guilty pleas in federal court and were scheduled for a bail hearing Thursday. They were charged with conspiracy to commit bribery, witness tampering, mail fraud and interstate transportation in aid of racketeering. If convicted, they could face up to 20 years in prison.