The man, named as Claudio Giardiello, 57, escaped on a scooter but was captured by police in Vimercate, a town some 25 kilometres outside Milan.
"This is a moment of great pain, of sadness," Prime Minister Matteo Renzi said, adding that it was "unthinkable someone should be able to enter a court with a weapon."
He promised "the government will shed the utmost light on the event" and described the police who had chased down and arrested the man as heroic.
The dead were named as judge Fernando Ciampi, and Giardiello's former lawyer, 37-year old Lorenzo Alberto Claris Appiani, who was in court as a witness in the case.
A third person was found dead on the stairs inside the building after apparently suffering a heart attack, probably brought on by the shooting, emergency services said.
The attack "has sparked great alarm and concern throughout the country", speaker Laura Boldrini told parliament.
Giardiello likely used a false pass to enter the court through a side door reserved for judges and lawyers, who are not obliged to have their belongings scanned by a metal detector, Milan public prosecutor Edmondo Bruto Liberati said.
He fired at his lawyer and one of his co-defendants during a hearing, before heading to the floor below, according to reports from court sources. There, he shot dead the judge, who had been trying to protect a female colleague.
Police said Giardiello fired 13 rounds in total. Two people were injured in the shootout and one is in a serious condition.
Giardiello was caught on video surveillance cameras fleeing the court and police traced him via his numberplate.
'Ready to kill others'
Interior Minister Angelino Alfano told a press conference it appeared the man "was ready to kill others in Vimercate," following reports Giardiello had been heading towards another co-defendant, who was not present in court.
Valerio Maraniello, who once acted as a lawyer for the shooter, told AFP his ex-client was "an aggressive person, a little paranoid."
"He was always convinced people were trying to cheat him. He never listened to advice," he said, adding that he was "not completely surprised" by the deadly attack.
Leaders across the political sphere expressed shock that a gunman had managed to get into the court building.
"The fact that we're not talking about an organization which surveyed the place first makes it even more perturbing," said the head of the Lombardy region, Roberto Maroni.
Alfano - in Milan for a meeting on public order and safety ahead of the 2015 Milan Expo which opens in May - promised to investigate what he described as "serious errors" in security.
"If this is the level of security in Expo Milan imagine what potential terrorists can think of doing," said Matteo Salvini, head of the anti-immigration Northern League party.
The sound of shots had sparked widespread panic, with lawyers bolting for the exit while police officers went floor to floor searching for the gunman, who was initially thought to be still inside the building.
"All of a sudden we heard at least three or four shots," lawyer Marcello Ilia told AFP outside the court.
"There were suddenly lots of police officers who told us not to leave the room. They told us someone in a suit and tie was armed and at large in the court," he said.
The Palace of Justice is in the historic centre of Milan, only a few streets away from the city's cathedral and shopping district.
Judge Gherardo Colombo told journalists outside the court that he had known the murdered judge well.
"It's absurd that one can die like that, while you're doing your job," he said, suggesting "an anti-judiciary climate" may be to blame.
Giardiello owned 55 percent of a real estate company which went under in 2008, media reports said.