The ban – imposed on the grounds that circumcision amounted to "bodily harm" – triggered an emotional debate over the treatment of Jews and other religious minorities, a sensitive subject in a country still haunted by its Nazi past.
The outcry prompted Germany's center-right government and opposition parties to draw up legislation confirming the practice was legal – overruling the decision by a court in the western city of Cologne.
The new law passed by an overwhelming majority in Bundestag lower house said the operation could be carried out, as long as parents were informed about the risks.
Jewish groups welcomed the move.
"This vote and the strong commitment shown ... to protect this most integral practice of the Jewish religion is a strong message to our community for the continuation and flourishing of Jewish life in Germany," said Moshe Kantor, president of the European Jewish Congress.
Germany's Catholic Bishops Conference said it hoped the bill would help safeguard religious freedoms. No comment was immediately available from the country's Central Council of Muslims.
The May ruling centered on the case of a Muslim boy who bled after the procedure and the ban only applied to the area around Cologne.
But some doctors in other parts of Germany started refusing to carry out circumcisions, saying it was unclear whether they would face prosecution.
Under the new law, a doctor or trained expert must conduct the operation and children must endure as little pain as possible, which means an anesthetic should be used. The procedure cannot take place if there is any doubt about the child's health.
Justice Minister Sabine Leutheusser-Schnarrenberger said no other country in the world country had made the religious circumcision of boys an offence.
"In our modern and secular state, it is not the job of the state to interfere in children's' upbringing," she said.
Child welfare group Deutsche Kinderhilfe disagreed, saying the government had "(pushed) through the legalization of the ritual of genital circumcision ... against the advice of child right campaigners and the medical profession."