The bill gives homosexual couples in Germany the same rights as heterosexual couples, and will allow same-sex couples to marry and jointly adopt children. It passed by 393 votes to 226, with four abstentions.
The bill is likely to pass through the Bundesrat -- Germany's upper house -- next week. The Bundesrat has previously approved legalizing same-sex marriage.
Once it has been officially signed into law, Germany will enter the club of more than 20 countries where same-sex marriage is legal.
Two-thirds of Germans said they were in favor of legalizing same-sex marriage in a recent YouGov poll
A crowd of same-sex marriage supporters who waited outside the parliament building as the vote was held cheered in celebration as the bill was passed, hailing a historic day for equal rights.
Merkel votes no
Speaking after the vote, Merkel said she had voted no on the issue, but that it had been important to put it to a vote of conscience.
"For me and the basic law, it's about the marriage of a woman and a man. That's why I voted against it," she said, adding that it had been an emotional debate for many in parliament, including herself.
"I hope that the vote today shows not only the mutual respect for different opinions but that this also leads to more peace and social cohesion as well," she said.
Martin Schulz, leader of the Social Democratic Party (SPD) -- the second largest party in parliament -- tweeted following the vote that "progress is possible" and said he was "happy for all the married couples to-be."
Speaking by Berlin's Brandenburg Gate, however, he cautioned that the law could still end up before the Federal Constitutional Court because some members of Merkel's conservative Christian Democratic Union (CDU) party view it as fundamentally unlawful and requiring a change to the constitution.
The vote was welcomed by the International Lesbian, Gay, Bisexual, Trans and Intersex Association in Europe, which suggested same-sex couples could be able to marry by the end of the year.
"After years of waiting and hoping, rainbow families in Germany will now receive equal recognition under the law -- this is a historic milestone that can inspire even more change for LGBTI people," said ILGA-Europe executive director Evelyne Paradis.
The German Foreign Office tweeted "congratulations to all gay couples who have been waiting to get married!"
A 'vote of conscience'
The vote caps a turbulent week in German politics. Facing pressure on the issue from the leaders of other political parties, Merkel announced Monday that she would like to see parliament move towards a "vote of conscience" on same-sex marriage, kicking off a chain of events that culminated in Friday's vote.
The Chancellor made the comment at an event in Berlin hosted by women's magazine Brigitte, where she was asked by a gay man in the audience whether he would be able to refer to his partner as "my husband."
Merkel responded by acknowledging the widespread support for gay marriage among German voters and suggesting a free vote in parliament, meaning lawmakers vote freely rather than being asked to support their parties' official positions.
Her comments represented a significant shift for the German leader and for her conservative CDU party, which has long opposed same-sex marriage and promoted "traditional" family values. Some in its more conservative Bavarian sister party, the Christian Social Union (CSU) reacted angrily to news of the vote.
Merkel under pressure
But with September's election looming, Merkel has come under increasing pressure, with her main rivals and potential coalition partners announcing support for "Ehe für alle" (marriage for all).
The left-leaning Green party and the centrist FDP had both said that they would not enter into any coalition deal with Merkel if "Ehe für alle" was not enshrined in it. The left-leaning Die Linke party has long supported full equality.
Friday was the last chance for parliament to vote on the issue before it breaks for the summer recess and election campaigning begins in earnest.
The result was welcomed by many of the country's liberal politicians on Twitter.
"Today, our country is more free, more tolerant, and more modern," said FDP leader Christian Lindner.
Green Party leader Cem Özdemir said the bill "doesn't take away anything, gives everything," while Katja Kipping, chairwoman of Die Linke party, hailed its passage as a moment "when many have the courage to finally decide on a matter of course." She added, "Now it is time for more courage."
Not all supported the step though.
Frauke Petry, former chairwoman of the right-wing AFD party, tweeted that Merkel "puts up marriage for auction on the altar of remaining in power. What did this country get into!"
Anja Neundorf, associate professor at the University of Nottingham, said she saw Merkel's change of stance as an election tactic designed to appeal to the average German voter -- but one that could backfire among traditional, religious CSU supporters in Bavaria. Merkel needs the support of those voters to win the election.
CSU supporters "are much more sensitive to this issue" than other voters, Neundorf told CNN. And some might choose to simply stay at home on polling day, she said.
By legalizing gay marriage, Germany follows the path of many of its European neighbors, as well as countries around the world.
In 2001, the Netherlands became the first country to legalize same-sex marriage. More than 20 other nations have followed suit, including Spain, Canada, Argentina, France, the United Kingdom and the United States.