By Anthony Boadle
BRASILIA (Reuters) - Brazil's ruling Workers' Party clinched a runoff for the mayorship of the country's biggest city on Sunday in municipal elections marked by fraying relationships with coalition parties crucial to its grip on federal power since 2003.
Fernando Haddad, the left-leaning party's candidate, dodged defeat in Sao Paulo after trailing a popular evangelical candidate and a veteran centrist in most of the polls before the vote.
Haddad was hand-picked to be a candidate by former President Luiz Inacio Lula da Silva and received support from current President Dilma Rousseff, as their party, known by its Portuguese initials as the PT, seeks to begin transferring power to a younger generation of leaders.
The 49-year-old former education minister will now face Jose Serra, a centrist former minister and presidential candidate, in a runoff in three weeks.
The mayorship of Sao Paulo, like the top office in other big Brazilian cities, is considered a launching pad for national office. Top municipal offices are also lynchpins of regional power in a country, Latin America's biggest, where democracy is marked by noisy and constant multiparty negotiations.
Sunday's elections, for mayors and local council members in 5,500 cities across Brazil, took place with the backdrop of a historic corruption trial involving former aides to Lula, who is still considered Brazil's most popular politician.
Voters also cast their ballots amid the most sluggish economy in once-booming Brazil since the PT came to dominate its political landscape.
Brazil's Supreme Court is expected to convict Lula's former chief of staff, Jose Dirceu, this week for running a vote-buying scheme that used public funds to shore up support for the PT-led coalition in the first two years of Lula's 2003-2010 presidency.
Opposition politicians wielded the scandal around the "mensalao," or big monthly payment, against PT candidates throughout the campaign.
One party leader, Governor Tarso Genro of Rio Grande do Sul state, called the trial a "political lynching" of the PT by Brazilian media, which covered the trial constantly. Genro said it had no impact on local election results.
After 12 years in power, the PT's governing coalition is having disagreements with allied parties that are fielding their own candidates in many cities. The tensions with long-standing allies made it harder to clinch cities like Sao Paulo and producing cliffhangers of several races.
The PT only won one mayoral race in a state capital outright, in the south central city of Goiania. In 10 of Brazil's 26 state capitals, the election of mayor will be decided in runoffs.
In Salvador, Brazil's third-largest city and the capital of Bahia state, its candidate, Nelson Pelegrino, appeared headed for a runoff against Antonio Carlos Magalhaes Neto of the conservative DEM party.
Many of the new tensions played out in the country's northeast, where strong economic growth over the past decade has energized cities previously considered political backwaters.
The center-left Brazilian Socialist party (PSB) won the mayor's race in Recife, defeating a PT senator, after the two parties fell out over who should run. The two coalition allies also fielded rival candidates for mayor in Fortaleza who will go head to head in a second round vote on Oct 28.
The PSB's strong showing in northern Brazil has increased speculation that its leader, Senator Eduardo Campos, could run for president in 2018 or possibly challenge an expected Rousseff re-election bid in 2014.
Rio de Janeiro, Brazil's second-largest city and a city rapidly preparing for the 2016 Olympic Games, re-elected Mayor Eduardo Paes of the PMDB party, a member of Rousseff's governing coalition.
About 140 million Brazilians voted in the first election under a new "clean record" law aimed at rooting out corruption by barring from elected office anyone convicted of murder, sexual assault, fraud, money-laundering or drug trafficking.
(Editing by Paulo Prada and Philip Barbara)