The 49-year-old centre-left candidate had won 35.2% of the vote with nearly all votes counted.
Mayor of Bogotá is widely considered the second-most important political post in Colombia after the presidency.
"Not only did we win but we're changing history," Ms López said on Twitter.
She narrowly beat liberal Carlos Fernando Galán, who received 32.5% of the vote.
Who is Claudia López?
A journalist-turned politician, Ms López is a member of the of the country's Green Alliance party. Known for her outspoken criticism of right-wing politicians, she has also been heavily involved with anti-corruption efforts in Colombia.
Her policies include putting more police on the streets, fighting child labour and creating better educational opportunities for people over 45.
Ms López's victory was also cause for celebration among the country's gay community. Her partner is Angélica Lozano, a fellow Green Alliance politician.
"This is the day of the woman," Ms López told supporters celebrating her victory in the capital. "We knew that only by uniting could we win. We did that. We united, we won and we made history!"
Renata Segura, associate director of the Conflict Prevention and Peace Forum and a friend of the couple, said on Twitter: "This is a momentous symbol, a sign of change and of good things to come."
What else happened in the elections?
Seven candidates were killed, a dozen were attacked and more than 100 received threats across the country, according to the electoral observation mission.
Despite the violence, the head of the national registry office said the polls were the most peaceful of recent local and regional elections.
Left-wing former guerrilla group Farc, which signed a historic 2016 peace agreement with the government after more than 50 years on conflict, fielded candidates in elections for a second time.
They won a majority in a coalition with two other parties in one municipal government in the country's southwest.
Two former Farc members have also been elected, however they were not running for the Farc political party.
One of them is Guillermo Torres, a former guerrilla known as the Farc singer for his performances of traditional Colombian folk music, who was elected mayor of Turbaco, a town in Bolívar department.
More about: Colombia