By Anthony Boadle

BRASILIA (Reuters) - Brazilian political parties implicated in the massive Petrobras corruption scandal, including that of President Michel Temer, suffered major setbacks in Sunday's municipal elections that put right-leaning candidates ahead in key cities.

The leftist Workers Party (PT) of former President Dilma Rousseff, who was impeached in August, was the worst hit and lost the country's largest city, Sao Paulo, to its main rival, the centrist Brazilian Social Democrat Party (PSDB), which elected millionaire businessman Joao Doria to be mayor.

Temer's Brazilian Democratic Movement Party (PMDB) lost its longtime hold over the cash-strapped city of Rio de Janeiro, which just held what many considered a successful Olympics.

Instead, a conservative evangelical bishop, Senator Marcelo Crivella, will face a runoff against Marcelo Freixo of the Socialism and Liberty Party (PSOL), a leftist breakaway from the PT, to decide who leads Rio.

Voters punished the PT, which ruled for 13 years, for holding the presidency during Brazil's biggest political corruption scandal and leading Latin America's largest economy into its worst recession since the 1930s.

"It was a clear sign of dissatisfaction of the voters, mainly in Sao Paulo and southeastern Brazil," said political analyst Luciano Dias, a partner in the Brasilia-based consultancy firm CAC.

Adding to the drubbing suffered by the PT, Doria won Sao Paulo in the first outright victory in the city's mayoral race since run-offs were introduced to the city in 1992.

The PT lost four of the five state capitals it had run, including Sao Paulo, the country's economic powerhouse where the leftist party was born. The PT lost two-thirds of the municipalities it won in 2012, dropping to 10th place from third in the number of mayors controlled by each party.

The PT's candidate in Sao Paulo's industrial suburb city of Sao Bernardo do Campo, the hometown of the party's founder Luiz Inacio Lula da Silva, did not make it to the second round, despite strong stumping by the former president.

Lula left office in 2011 with the highest approval rating of any Brazilian leader but will now stand trial for corruption in the massive bribery and political kickback scandal at state-run oil company Petroleo Brasileiro.

The first elections since Rousseff was removed from office were a test of support for Brazil's main political parties as they prepare for the 2018 presidential race.

The local elections do not bode well for the PMDB, which hopes Temer can revive the economy so the party can win the 2018 elections. But with some of its leaders under investigation, voters have chastised the PMDB for being part of both Lula and Rousseff's PT-led coalition since late 2006.

Despite the PMDB's big loss of Rio de Janeiro, Brazil's second-largest city, the party continued to be the one with the most number of mayors across and small and medium-sized towns, winning in at least 1,000 such locations.

Doria's victory in Sao Paulo, a traditional launching pad for national office, will bolster a likely bid in 2018 by the PSDB governor of the state, Geraldo Alckmin. The PSDB was also ahead in the country's third-largest city, Belo Horizonte.

Despite the PSDB win in Sao Paulo, voters also expressed deep indifference toward their political options, with nearly 40 percent either not showing up at the polls - despite voting being legally mandatory in Brazil - or refusing to vote for any mayoral candidate.

Sunday's elections were the first held under a ban on corporate campaign financing that was meant to clean up Brazilian politics following the scandal surrounding state-controlled oil company Petrobras that has ensnared dozens of top executives and powerful political figures.

But the new rules, which reduced campaign financing by two-thirds from the presidential election in 2014, instead helped wealthy candidates who were using their personal funds, such as Doria, and candidates backed by Brazil's rapidly expanding evangelical churches.

(Reporting by Anthony Boadle; Editing by Peter Cooney and Brad Brooks)