Raggi, a 37-year-old lawyer whose campaign for the Five Star party tapped into discontent over a string of corruption scandals in the Italian capital, took 36 percent of the vote.
In third place in Sunday's poll was Giorgia Meloni, a candidate put up by one of several small groups that emerged from Italy's neo-fascist movement.
Meloni was backed by the anti-immigrant Northern League, whose leader Matteo Salvini wants to unite all the right behind him.
Silvio Berlusconi had pinned his hopes on Alfio Marchini, who came a distant fourth in a blow to the former prime minister as he jostles with Salvini for influence on the right.
“Romans are sending a clear message. We are witnessing a historic moment,” Raggi said in a nighttime address two weeks before potentially becoming first female mayor of the Eternal City.
Rome was considered the main prize in a battle for municipal seats across the country Sunday, in local polls widely seen as a test for Renzi.
A PD official still celebrated the second-place result as a “miracle” for the party, which was in a very bad position at the kick-off of the campaign.
The Italian capital has been without an elected leader since last October, when Ignazio Marino, a member of PD, was forced to quit over an expenses scandal.
That episode and a much bigger, unrelated scandal over organized crime's infiltration of City Hall have bolstered Raggi's bid.
Even if Raggi does not win, her strong showing is likely to boost the populist Five Star movement, led by comedian Beppe Grillo, as it seeks to cement its status as a mainstream party.
They will be hoping that success in Rome will give them the platform they need to transform themselves into Italy's principal opposition in the run up to national elections due by June 2018 at the latest.
Losing control of Rome would not augur well for Renzi four months before a referendum on constitutional reforms designed to end decades of gridlock in parliament.
Renzi has vowed to resign if voters reject the reforms.
Eyes were also trained on the mayoral election in Milan, where centre-left candidate Giuseppe Sala took a narrow lead with more than 41.6 percent of the vote.
He was just ahead of the centre-right's Stefano Parisi on 40.9 percent.
Overall, more than 13 million people nationwide were asked to cast their ballots to choose members of 1,300 municipal councils in a two-round ballot.
Perhaps concerned about the impact of losing both Rome and Milan for the momentum of his reform programme, Renzi has played down the significance of the local elections.
As if to demonstrate his insouciance, the 41-year-old visited the training camp of the Italian football squad on Sunday to wish them luck in the Euro 2016 championships starting in France next week.
“The municipals are about mayors, the people whose job it is to repair the streets, not the government of the country,” he said ahead of the vote.
PD candidates came out ahead in Turin and Bologna, while in Naples current mayor Luigi di Magistris, an independent candidate and fierce critic of the government, topped polls.