The three were part of a group of five gangsters from the GTA street gang, who tried to seize the guards' weapons in an elevator, Russia Interior Ministry spokeswoman Tatiana Petrova told state-run news agency TASS. They were defendants being taken to trial at the Regional Court in the outskirts of Moscow at the time, she said.
"Three of the arrested were killed while trying to flee the scene when the elevator was forcibly stopped, two were wounded," Petrova said.
A bailiff and armed guard also suffered bullet wounds in the attack, lawyer Sofia Rubasskaya, who was in the building at the time, told TASS. At least two policemen were also wounded.
"We were at a different session when heard shots fired. More than 20 shots were made. We began to be evacuated. I saw a wounded woman bailiff, her face in blood. Later somebody said that an armed guard had been injured," Rubasskaya said.
The incident is not thought to be terrorist related and the court has resumed its hearings, TASS reported.
The members of the GTA Gang, which took its name after a computer game, are charged with the murders of car drivers in the Moscow and neighboring Kaluga regions in 2012 and 2014, Russian media reports.
CNN's Angela Dewan and Carol Jordan contributed to this report.