Relieved party chiefs thanked 80-year-old Mattarella for agreeing to remain, but the failed attempts to replace him during seven rounds of balloting have left deep scars, with potentially dangerous repercussions for political stability.
Nonetheless, financial markets are likely to react positively to the status quo, which will see Prime Minister Mario Draghi, who had made clear he hoped to become president himself, continuing as prime minister instead.
Draghi said in a statement that Mattarella's re-election was "splendid news for Italians," thanking him for "his decision to go along with the extremely strong will of parliament."
Pope Francis sent the re-elected president a telegram of congratulations.
At the eighth round among more than 1,000 lawmakers and regional delegates in the Chamber of Deputies, loud and prolonged applause broke out when Mattarella passed the 505 votes needed for election.
He had previously ruled out remaining in office, but with the country's political stability at risk he changed his mind in the face of appeals from parliamentary leaders who met him at his palace earlier in the day.
In brief comments from the palace, Mattarella said the ongoing coronavirus crisis and Italy's difficult economic and social conditions meant he was duty bound to accept the decision of parliament.
He said that even though he had had other personal plans, he was "committed to matching the expectations and hopes of the people".
In Italy's political system, the president is a powerful figure who gets to appoint prime ministers and is often called on to resolve political crises. Governments in the euro zone's third-largest economy survive around a year on average.
The leader of the centre-left Democratic Party (PD) Enrico Letta, who had championed Mattarella's re-election, spoke to reporters to express "enormous thanks ... for his generous choice towards the country."
Draghi earlier called Mattarella and urged him to stay on, a political source said.
Relations among the parties in the ruling coalition have deteriorated during the election process amid mutual recrimination over the failure to find a consensus figure.
Draghi's coalition includes the main centre-left and centre right parties as well as the right-wing League, the once anti-establishment 5-Star movement and a range of smaller parties.
"The overall political backdrop has become less supportive for Draghi's government, which is facing a daunting task in the year or so left before the next general election," said Wolfango Piccoli of political risk consultancy Teneo.
On the right, while both the League and centre-right Forza Italia in the end embraced the appeal for Mattarella to continue, their ally Brothers of Italy, which has not joined them in government, denounced the behind-the-scenes manoeuvring.
"Parliament has shown it is not fit for Italians," said Brothers of Italy leader Giorgia Meloni in a statement.
She accused her allies of "bartering away" the presidency to ensure the government remains in place until the legislature ends in 2023, and said the conservative bloc "needs to be re-founded."
PD chief Letta, who has emerged stronger from the week of cross-party confrontation, said "the political landscape has changed," and some observers are forecasting changes in Draghi's cabinet team in the near term.
Even before the deal among their leaders, lawmakers had been increasingly backing Mattarella in the daily ballots, with his tally rising to 387 in the seventh round earlier on Saturday.
In the end he got 759 votes, 94 more than at his first election in 2015 and the second highest tally for any Italian head of state after Sandro Pertini, president from 1978 to 1985.
(Writing by Gavin Jones and Crispian Balmer; Additional reporting by Philip Pullella; Editing by John Stonestreet, David Holmes, Peter Graff and Daniel Wallis)
By Angelo Amante, Giuseppe Fonte and Gavin Jones