Lopez, who was supported by a center-left coalition, defeated the center-right Carlos Galan who ran as an independent with considerable support from the private sector.
The Green Alliance politician received 35.2% of the votes, defeating her direct opponent who received 32.5%.
Having ended second, Galan will be granted a seat in the Bogota council.
Former journalist Hollman Morris, a long-time ally of opposition Senator Gustavo Petro, ended third with 14% of the votes.
Bogota election results
Source: National Registry
Lopez’ victory is the latest boost for her opposition party, which has been gaining political power for years while on a political crusade against corruption, nepotism and extremism.
Bogota’s mayor-elect will also become the first openly gay executive in the largely Catholic country, making her a formidable counterweight against the Duque, who has been promoting a staunchly social conservative agenda.
Last but not least, Lopez has been a vocal promoter of the country’s peace process, which is fiercely opposed by the president’s far-right party.
Her victory is another devastating blow for Duque who enjoys the support of Bogota’s outgoing mayor Enrique Peñalosa.
Lopez, a political scientist, is an experienced politician whose career took off in the mid 2000s after she revealed far-reaching ties between her country’s traditional ruling class and far-right paramilitary groups.
Since then, she has become one of the most-feared political forces of the regional clans that have been controlling and corrupting Colombia’s political system since the beginning of the republic.
After Lopez led the Green Alliance in the senate between 2014 and 2018, her party became one of the fastest growing forces in Congress and the leading force in the opposition against the president and his minority coalition.
The new Bogota mayor will take office on January 1 next year.