Cape Town Mayor Patricia de Lille on Thursday survived a motion of no confidence against her by her own party, the Democratic Alliance, (DA) by a single vote.
The motion was defeated by 110 votes to 109, with three abstentions.
This result comes after De Lille headed to the Western Cape High Court this week for an urgent application seeking to have the vote of no confidence in her conducted by secret ballot.
The opposition African National Congress (ANC) rejoiced in the chamber as the news of De Lille's narrow victory emerged.
Afterwards, De Lille tweeted: "Thank you to the councillors for their support. You showed your courage and conviction. Let’s get back to our work!"
De Lille earlier walked out of the council sitting when its speaker Dirk Smit refused to allow the secret ballot.
De Lille's legal team argued on Wednesday that a secret ballot was the only way to protect councillors from DA bosses who they feared might intimidate or even fire those who voted against the motion.
The court had left the final decision in the hands of the Speaker but despite Smit ruling that the vote not be held in secret, De Lille managed to survive the motion.
The grounds of the no-confidence motion were based on the allegations of maladministration and corruption against De Lille.