In total 393 MPs voted for and 226 voted against.
The law was pushed through by the left-wing parties in the Bundestag. But at least 70 MPs from Angela Merkel’s conservative CDU/CSU party union voted for the bill. Merkel herself told reporters on Friday after the debate that she had voted against the bill.
Volker Beck, Green Party MP and one of the most prominent gay parliamentarians, said “this really is a great victory, because it inches us towards a harmonious society.”
With tears in his eyes, Beck told broadcaster Phoenix that “there had almost been a crisis of democracy”, referring to the fact that same-sex marriage legislation took years to come to a vote despite polls showing that a majority of Germans supported it.
The German legal code was now changed to say "marriage is entered into for life by two people of different or the same sex", meaning same-sex couples will gain equal rights on adoption, as well as tax and inheritance.
The law has already been approved by the upper house and should come into effect by the end of the year.
"To me, marriage as defined in the German basic law means the marriage between husband and wife, and that is why I vote against the law today," Merkel said shortly after the vote.
But she did say that her thinking had changed on the question of child adoption by same-sex couples, which she long opposed, labelling her past comments on it "unsatisfactory".
"Since then I have thought a lot about the matter of child welfare and have now... come to the conviction that same-sex couples should be able to jointly adopt children," she said.
Gay and lesbian groups cheered the push for marriage equality in Germany.
"It's a real recognition, so it warms the heart," said French engineer Christophe Tetu, 46, who lives in Berlin with his partner Timo Strobel, 51.
"We're thinking about having a party, getting married and using our new rights to protect our relationship," he told AFP.
Strobel said he too was "overjoyed" the couple would be able to show family and friends "that we are committed to each other, that we will stay together and we will spend our lives together".
On social media, there was an outpouring of joy from supporters of the bill who could scarcely believe the speed with which it was put on the agenda and then passed through the Bundestag.
Despite the overwhelming public support for gay marriage, it is not completely certain that the draft law will pass onto the statute books.
Horst Seehofer, leader of the conservative Bavarian CSU party, said on Thursday he would look into whether the law conforms to the German constitution.
Some legal experts believe the constitution will need to be altered before the law can come into effect.
The hard-line conservative Alternative for Germany (AfD) also criticized the law, calling it “a serious societal failure.”
Party deputy Alexander Gauland said that marriage between a man and a woman was “a cultural core of western Christian society.”
He went on to warn that the “arbitrary values” of mainstream politics were “dissolving our societal structures.”