One the victims, who was not identified, told Rai News24 from his hospital bed that he feared being killed as the attacker fired. He lifted a sheet to show his leg, wrapped in bandages.
The shootings rattled a nation where such episodes are rare. According to a 2007 study, about 12.9 percent of Italian households have guns.
The rampage came days after the brutal killing of an 18-year-old woman, Pamela Mastropietro, in Macerata, a crime that shook the city of 42,000 people east of Rome.
Ms. Mastropietro’s body was found in pieces in two suitcases on Wednesday, and the police arrested a Nigerian man, Innocent Oseghale, the next day. He stands accused of killing her and dismembering her corpse. The arrest quickly became a focal point for anti-migrant sentiments.
On Saturday, officials told reporters there was no indication that Mr. Traini and Ms. Mastropietro had known each other. But Mr. Minniti, the interior minister, said Saturday’s shooting “strongly recalls a raid of retaliation — a random, armed retaliation.”
Macerata’s mayor, Romano Carancini, said the connection “may not have been direct, but could be ascribable to the campaign of racial hatred that began after Pamela’s death.”
“Nothing so serious had ever happened in Macerata,” at least not in living memory, Mr. Carancini said in a telephone interview about Ms. Mastropietro’s killing. “Pamela’s death upset and struck a chord in the city,” he said, adding that the racial hatred that followed “filled people with fear.”
Prime Minister Paolo Gentiloni told reporters after the shooting that the government would deal “severely” with anyone trying to stoke racist actions. “Hatred and violence won’t be able to divide us,” he said.
There are just over five million foreigners residing legally in Italy, about 8.3 percent of the population of 60 million. Of these, 3.7 million are from outside the European Union.
Immigration is a hotly contested issue in Italy with national elections a little more than a month away. Far-right leaders have tried to capitalize on national unease about the unregulated waves of migrants who have made the perilous crossing of the Mediterranean in recent years.
Matteo Salvini, the leader of the League political party, which has historically run on an anti-immigrant platform, said on Twitter after Ms. Mastropietro’s killing that the suspect “was not running from war; he brought war to Italy.”
“The left has blood on its hands. Expulsions, expulsions, controls and more expulsions,” he wrote.
Italian media reports said that the suspect in the shootings on Saturday had been a League candidate in municipal elections last year in the nearby town of Corridonia. He was not elected.
On Saturday afternoon, a sit-in organized in support of Ms. Mastropietro’s family by associations that work with migrants and members of the city’s Nigerian community was canceled after the shootings.
“Now we are afraid,” Sammy Kunoun, an organizer, told ANSA.