Members of lower house of parliament the Bundestag passed the cross-party motion with 434 votes for the measure, 100 against and with 46 abstentions.
The legislation states that male infants may be circumcised for religious reasons within six months of their birth by a "specially trained" practitioner, although this person does not have to be a doctor.
Nevertheless, the rite must be carried out "in a professional medical manner," the law states and stipulates exemptions for infants who could be at risk from the practice such as haemophiliacs.
The legislation sought to clear up confusion after a judgement published in June by the regional court in the western city of Cologne.
While considering a case brought against a doctor who had circumcised a Muslim boy, the court ruled the rite was tantamount to grievous bodily harm. The decision united Jewish and Muslim groups in opposition and caused outrage from religious and political leaders in Israel and Muslim countries.
"I am pleased and relieved about the decision of the German parliament," said Dieter Graumann, president of the German Jewish Council.
The law "finally restores legal certainty and hopefully ends the often unfortunate debate that took place in 2012," he added. German politicians had acted "quickly and responsibly" and they deserve "praise and respect" for this, Graumann said.
Diplomats had admitted that the ruling proved "disastrous" for Germany's international image, particularly in light of its Nazi past.
Story continues below…
Merkel was reported to have warned that Germany risked becoming a "laughing stock" if it banned circumcision.
Germany is home to about four million Muslims and more than 200,000 Jews.