Zekra Alwach, a civil engineer and director general of the ministry of higher education, becomes the first female to be given such a post in the whole country, where international rights groups have condemned women's rights abuses.
As mayor -- the most important administrative position in the capital -- Alwach will deal directly with Prime Minister Haider al-Abadi and holds has the prerogatives of a cabinet minister.
She will begin work Sunday, according to a municipal source.
"Abadi sacked the (former) mayor Naim Aboub and named Dr Zekra Alwach to replace him," government spokesman Rafed Juburi said.
Signup to our newsletter and follow us on Facebook and Twitter!
Aboub's removal was not designed as a punishment, although he was regularly accused on social media and by Baghdad residents as incompetent, the spokesman added.
He made headlines in March 2014 when he described his city, beset by brutal sectarian violence and rife with corruption, as "more beautiful than New York and Dubai".
"Aboub is a clown. Abadi should have sacked him from the start," said Yasser Saffar, a Baghdad baker. "All his statements were ridiculous."
Alwach's appointment is a breakthrough for gender equality in Iraq, where rights groups say discrimination and violence against women is widespread.
According to a UN report last year, at least a quarter of Iraqi women aged over 12 are illiterate and just 14 percent enter the world of work.
Baghdad is currently plagued by car bombings and sectarian killings, and militants from the Islamic State group have seized much of Anbar province to the west, menacing the capital.