ROME — Two military police officers and a passer-by were shot and wounded on Sunday in a crowded square outside the office of Prime Minister Enrico Letta and near the presidential palace, where his new government was being sworn in.

The shooting was shown live by the state broadcaster RAI, which had a television crew in the square in front of Mr. Letta’s office, Palazzo Chigi, where the new ministers were to go after the swearing-in ceremony.

What was supposed to be a day of celebration, marking a government that took nine weeks after the elections to assemble, quickly turned into a national drama. The square in front of Palazzo Chigi was cordoned off, and ambulances and police cars blocked traffic in one of Rome’s busiest downtown areas. Inside the palace, the ceremony continued undisturbed, and most of the ministers were not told of the shooting, which occurred about half a mile away, until after the ceremony.

A man, identified as Luigi Preiti, who is unemployed and is from the Calabria region, was detained and accused of the shooting, the authorities said.