Against an alarming background of violence inside and outside parliament, Silvio Berlusconi today scraped through confidence votes in both houses of the Italian parliament.

The survival of his rightwing government was greeted by widespread disturbances in Rome where hooded and helmeted protesters set up flaming barricades, attacked police with sticks and bars, smashed the windows of shops and banks, and set alight cars, police vans and local authority vehicles. Police responded with baton charges, teargas and, in some cases reported by witnesses, indiscriminate beatings.

Ninety people, including 50 police, were reported injured. According to police, there were 41 arrests.

Inside parliament politicians almost came to blows and had to be separated by ushers as uproar broke out after one deputy who had been expected to back the opposition switched her vote at the last minute. According to one contested account, Catia Polidori was called a "whore" by another politician, who was then attacked by a deputy loyal to the government.

Polidori and two other former rebels secured for the prime minister a 314-311 vote in the chamber of deputies, the lower house. Berlusconi also won a confidence vote in the senate, by 162-135.

His path through the upper house was smoothed by the tactical abstention of a rebel group loyal to his former deputy, Gianfranco Fini, whose revolt first pitched Italy into crisis in July. For Fini, the result of the day's ballots was a stinging rebuff and one that is bound to raise doubts about his tactics and judgment.

Questions will also be asked about how, in several parts of Rome, police were apparently caught unawares by demonstrators who split away from a march involving tens of thousands of people protesting against the government and the effects of the recession.

Opponents of the government, including trade unionists and revolutionary socialists carrying red flags, were joined by students demonstrating against a recently approved university reform bill and people left homeless by the L'Aquila earthquake last year. The marchers filled the broad, long avenue that runs from the Colosseum through Rome's ancient forums.

But as they approached the labyrinthine historic centre, where both houses of parliament are located, there were already warnings of the violence to come. Several explosions rocked the city as groups of demonstrators broke away from the march to detonate homemade firecracker bombs.

One was thrown at a government politician as he left parliament. Police and protesters soon clashed in several parts of Rome, leaving several areas of the city strewn with burned-out cars, shattered glass, paving stones and rubble from barricades that had been thrown up by demonstrators and broken down by police.

In the broad Piazza del Popolo, the scene of some of the most violent clashes, two thick pillars of smoke rose from the remains of a barricade and mingled with teargas fired to disperse the protesters. Student demonstrations were also held in several other cities, including Milan where they briefly occupied the stock exchange.

Berlusconi's opponents in parliament failed to unseat the prime minister despite the efforts of three women deputies in the last stages of pregnancy who turned up to cast votes against the government. One, Giulia Cosenza, arrived in an ambulance. Another, Giulia Bongiorno, was helped into the chamber in a wheelchair.

The third, Federica Mogherini of the Democratic party, Italy's biggest opposition group, who is nine months pregnant, won a round of applause from her colleagues after fulfilling a promise to get to parliament "unless my water breaks".

Adding to the tension on a day of high drama, the prime minister and most of his followers walked out of the lower house in the final stages of the debate in protest at the heated rhetoric of Berlusconi's most implacable enemy, the leader of the Italy of Principles party, Antonio Di Pietro.

"We have a prime minister derided and ridiculed abroad," said Di Pietro, who went on to allege that Berlusconi had "bought opposition deputies to assure himself of a majority". Two politicians from his party switched their votes in the runup to the ballot.

Pier Luigi Bersani, the leader of the Democratic party, said after the result was known that the prime minister had won a "pyrrhic victory" by resort to "totally scandalous vote trading". Several commentators noted that the governing coalition's tally fell short of an absolute majority and that, with Fini's group now firmly in the opposition, faced serious difficulties in passing legislation.

But Berlusconi said tonight: "There is no alternative majority, so one must carry on." He added that he would try to broaden his majority, citing as one target the conservative Christian Democrat Union of the Centre (UDC). Since Fini's revolt he has depended exclusively on his own party, the Freedom People (PdL) and the Northern League.

"There are parliamentarians who after today could come back into the PdL's parliamentary groups or, at any rate, into our broader coalition", Berlusconi said. "I am thinking of the UDC and other parties that do not want to stay in a corner."

The Northern League, which has argued for an early election, had been thought likely to object to the Christian Democrats' inclusion. But its leader, Umberto Bossi, said: "There is no veto on the UDC."

The key votes

A bitterly disappointed Gianfranco Fini said his defeat was made "even more painful" by the last-minute decisions of three of his deputies to support the government. One unexpectedly left the chamber. Two voted with Berlusconi.

Without their changes of heart, the vote would have gone the other way. Fini knew some supporters were queasy about bringing down the government, but thought he had persuaded them with an appeal to Berlusconi to resign and form a new administration with a different programme.

The vote stirred claims of corruption by Italy's embattled billionaire prime minister. A deputy for the centre-left Democratic party (PD), the biggest opposition group, opted to support the government in the runup to the vote, as did two from the Italy of Principles party, whose party leader, Antonio Di Pietro, has asked prosecutors in Rome to investigate.

In the senate one of four members who unexpectedly switched votes at the last minute was expelled by his party, the Sicilian-based Movement for Autonomies. His whip said: "This buying and selling [of votes] … has turned parliament into a sort of cattle market."

John Hooper

• This article was amended on 15 December 2010 to complete the whip's quote and to give the journalist's byline.