For nearly a dozen years, Puerto Rican-born Mayor Maurice A. Ferre not only survived the ethnic wars among Miami's black, white and Cuban-American communities, but in many ways stayed in office because of them.

Some saw the suave, 50-year-old architect as an essential bridge between feuding city factions. Others viewed him as a political beneficiary of the rifts among the three groups, skillfully frustrating the hopes of any one group to have him replaced in the highly symbolic but fiercely coveted $6,000-a-year job.

Now Ferre, dubbed a "political Houdini" by one former supporter because of his ability to escape seemingly inevitable defeat, is bidding for a seventh two-year term. And his two biggest problems are the Cubans and blacks, the largest voting blocs in this fast-changing city.

Even some of Ferre's opponents see the possibility of yet one more escape from defeat in the Nov. 5 nonpartisan election and in the almost certain runoff a week later. But the race is considered virtually wide open as campaigning begins in earnest.

The past year has seen a shift in the base that elected Ferre. Blacks, who gave him 97 percent of their vote two years ago, now are essentially split three ways. Cuban Americans are giving much of their support to Ferre's two Cuban-American challengers.

The splits, many believe, leave the mayor with enough support to make it through the election as one of the two top vote-getters, but not enough to win outright.

"He's probably the most gifted political figure in this country," said educator Marvin Dunn, one of the three leading challengers. "He's a survivor. He could survive this . . . . But the strongest leg of his triad is the black vote, and with the 'Gary Affair' he cut one of his legs or at least he severely damaged it."

The "Gary affair" occurred Oct. 25, 1984, at Robert E. Lee Junior High School on the outskirts of Liberty City, the mostly black community on Miami's north side that exploded in rioting four years earlier.

Ferre voted with the two Cuban Americans on the five-member City Commission to fire City Manager Howard V. Gary, a hometown black hero who was the highest paid municipal official in the nation ($108,000 a year) -- not to mention the first of his race in the job.

The official reason for the firing was Gary's lack of effective communications. The widely accepted unspoken reason was a clash of strong wills, especially after Gary fired the city's white police chief, a target of heavy black criticism. Unforgiving black leaders say Ferre sandbagged them by joining in the firing. Rank-and-file blacks say worse.

The incident made Ferre what one observer termed "a marked man" in the eyes of many Miami blacks. "There is a lingering rage against Maurice Ferre," said Francena Thomas, a university administrator and a columnist for the Miami Times, a black weekly.

Only a year before, blacks gave Ferre an estimated 97 percent of their votes in a bitter runoff. A record black turnout was galvanized in part by a radio crusade warning of a "Cuban takeover" if lawyer Xavier Suarez won. Harvard-educated Suarez finished a scant 201 votes behind Ferre in the primary, but got only 45 percent of the runoff.

Ferre said the broadcasts came from unauthorized supporters, but many Cuban Americans and whites blame him for exploiting racial divisions. Traditionally, he has stayed in office by drawing at least a healthy minority of votes from each of those groups to combine with a majority of black votes.

"For years, he was bordering on great for this community," said lobbyist and former Ferre supporter Steve Ross. "Now, in order to hang on to what he's got, he'll do almost anything. With the kind of tinderbox that you have in Miami, you can't have that."

Ferre counters: "I have never used race as a tool or instrument of political advantage . . . . I did not invent racial tensions, racial divisions in Miami. Ethnic politics exists in America and that is an issue that is as old as the republic. There's nothing that anyone is going to do about that."

Much of the big money in this year's contest -- primarily from the downtown business community -- has shifted from Ferre to Cuban-American banker Raul Masvidal, 43, a political newcomer who has raised about $400,000, twice as much as Ferre, but is still struggling to build name recognition.

Suarez, 36, is running again, with a record of no wins in his three previous political outings, two of them for city commission. "He's about to become the Harold Stassen of Miami politics," Masvidal quipped. But it is Suarez who has the strongest base in the Cuban community, and name recognition that far surpasses Masvidal's.

Dunn, the educator, 45, is hampered by a political base outside the black community and is running a low-budget campaign with little support from established black leaders.

Many of that establishment, including Gary, who considered running himself but said he bowed out because the salary was too low, are supporting Masvidal. He has a long record of involvement in Miami's black neighborhoods, including membership on the board of the local Urban League.

But what is attractive to blacks about Masvidal is suspicious to the Cuban Americans, even though the candidate wears what that group considers the ultimate badge of pride -- he was a participant in the 1961 Bay of Pigs invasion.

In Miami's Cuban-American community, said 28-year-old banker Ernesto de la Fe, "You have to be right of right to be center. Raul is more of a center of center. Therefore, a lot of people have problems with him."

In some respects, the mayor's job is small potatoes in Miami, the nation's 37th largest city, with a population of 372,364. Technically, it is a part-time position and, in the city's "weak-mayor" government, carries only marginally more power than that of the other four commissioners. Each of them is elected to four-year terms. The mayor serves for only two, but makes $1,000 a year more.

But thanks in part to Ferre's performance, the office has assumed great symbolic prestige for Miami's two largest ethnic communities. Hispanics, most of them Cuban, make up about 58 percent of the population, while blacks represent 24 percent.

"It may not mean anything in a constitutional sense or a legal sense. But the perception is whichever ethnic group is holding the office, that community is going to be favored," said former city attorney George F. Knox, who is black.

There is no white candidate in the mayoral contest, and that voting group is thought of, and mostly referred to, not as whites but as "anglos."

"Little Havana," a major Hispanic area, is by and large a solid middle-class community. It is dotted with transplanted Cuban institutions, such as the "American Club," once a haven for American businessmen and upper-crust Cubans in pre-Castro Havana, and now crowning a 14-story office building at Little Havana's main intersection.

Similarly, Masvidal's alma mater, Belen Jesuit Preparatory School, was founded in Cuba in 1852. Castro expelled the Jesuits from Cuba in 1960, and the school is now located in an unincorporated area of western Dade County. The enrollment is 850 boys, 90 percent of them Cuban American.

Not a single student at Belen is black, school officials said, and that is symbolic of the differences between the two communities that so often lead to feuds.

Black Miamians have long felt overshadowed and forgotten by the Cuban Americans' rise to prominence. One community is laced with poverty, the other with affluence. "We're political refugees rather than economic immigrants," Cuban-American investment banker Peter Gonzalez reminds a visitor.

National issues often divide the two groups most. Cuban Americans' adulation of President Reagan's staunch anti-Castro, anticommunist stance is as strong as blacks' hatred of Reagan's domestic policies and his support of South Africa.

Ideological differences tend to be underscored by language in a city where speaking Spanish is almost a necessity. At a recent mayoral forum on a Spanish-language television station, black candidate Dunn debated through an interpreter.

Yet Dunn is obsessively playing down race in his candidacy and Suarez and Masvidal are actively courting black voters. "The two Cubans who now have a chance . . . are not the rabid right-wing radicals," said Guillermo Martinez, a member of the editorial board of The Miami Herald, the city's largest daily newspaper. "There is a feeling in the community of putting their best foot forward."