The outcome of the municipal polls reshapes the political landscape in South Africa ahead of national elections due in 2019.
The African National Congress (ANC) has ruled virtually unopposed since it ended white-minority rule in 1994 with Nelson Mandela at its helm. But its grip on power is being shaken against the backdrop of high unemployment, a stagnating economy and a series of scandals that have dogged President Jacob Zuma.
With 99 percent of votes counted from Wednesday's municipal elections, the ANC was still leading in the overall count but had lost to the main opposition Democratic Alliance (DA) in the municipality of Nelson Mandela Bay, which includes the city of Port Elizabeth, a key manufacturing hub and port city.
The DA was narrowly ahead in Tshwane, home to the capital Pretoria, while the ANC held a slim margin in the country's economic hub of Johannesburg, in tight races that have changed hands several times during the vote count.
The outcome of the municipal polls reshapes the political landscape in South Africa ahead of national elections due in 2019 and they may also embolden Zuma's rivals within the ANC to challenge him.
The DA, which last year elected its first black leader, Mmusi Maimane, as part of a drive to shake off its image as a party that mainly serves white interests, retained control of Cape Town, which it has held since 2006.
"It signals to everyone that the tide in our country is turning," Maimane told reporters on Saturday, predicting that his party would edge out the ANC in Tshwane and form a coalition to run the municipality.
The ANC chairman in Gauteng province, which includes both Tshwane and Johannesburg, said his party was also seeking to form a coalition to govern both municipalities, adding there was likely to be no outright winner in either of the urban areas.
"We can confirm that we are into (coalition) negotiations as we speak," said Paul Mashatile, who blamed the poor result on low voter turnout.
"It's quite clear that our people, our traditional supporters are still with us but maybe not too many people came out to vote so we need to go back and find out why," he added.
The ANC has lost support among voters who feel their lives have not improved and the opposition has accused Zuma of mismanaging the economy. Millions of urban voters are now looking beyond its liberation struggle credentials and focusing on an economy teetering on the edge of a recession.
"The ANC may just become a rural party," said William Gumede, head of the Democracy Works Foundation, a think-tank.
Zuma's office said he would attend the official announcement of the municipal election results later on Saturday in Pretoria.
Zuma rattled investors in December by changing finance ministers twice in a week, sending the rand currency plummeting. The rand has since recovered and received a boost from the lack of violence during the local elections.
Zuma survived an impeachment vote in April after the Constitutional Court said he breached the law by ignoring an order to repay some of $16 million in state funds spent on renovating his private home. Zuma has since said he will repay some of the money as ordered by the court.
The radical leftist Economic Freedom Fighters (EFF) party led by Julius Malema, Zuma's one-time protege but now arch-foe, was running a distant third in the local elections, with about 10 percent of the vote.
Malema has drawn support with promises to redistribute among poor black people wealth still mostly in white hands - policies that both the DA and the ANC have not found palatable.
But neck-and-neck races between the ANC and DA in Johannesburg and Tshwane mean the EFF could be needed for coalitions there. Malema has not said whom he would back.
"If anyone comes to us, we'll talk," Malema told reporters when asked whether he would join a coalition.