Berlin took the measure, one of the toughest in the world, after a surge in racist and incendiary speech online, particularly since the arrival of around one million asylum- seekers since 2015.
Under German law, Holocaust denial, incitement of hatred, and racist and anti-Semitic speech are illegal.
But critics warned that the prohibitive fines would stifle legitimate free speech by prompting online giants like Twitter and Facebook to excessively delete and censor posts as a precaution.
But Justice Minister Heiko Maas argued that "freedom of opinion ends where criminal law begins".
More From This Section
"Death threats and insults, incitement to hate or (Holocaust denial) are not part of freedom of expression -- rather, they are attacks against other people's freedom of opinion," Maas said in a statement before the bill passed the lower house on the last day of the session.
"They are intended to intimidate and mute others," he said.
Under the new law, companies like Twitter and Facebook would have 24 hours to remove posts that openly violate German law after they are flagged by users.
The government emphasised that the heavy fines would be imposed only if companies systematically failed to follow the new directive, and not for individual cases.
Social media companies pledged in 2015 to examine and remove within 24 hours any flagged hateful comments, but a government report in April tracking progress on this front found that not enough had been done.
"The online platforms are not taking adequate action. Our experience has clearly shown that without political pressure, the social networks will unfortunately not budge," Maas said.
Google's YouTube video sharing platform fared far better, with a rate of 90 per cent, according to a government study cited by the minister.
Beyond hate speech, the legislation also covers other illegal content, including child pornography and terror- related postings.
A civil society umbrella called Alliance for Freedom of Opinion published a statement opposing the bill.
"Service providers should not be entrusted with the government's job of deciding whether content is legal," said the group, which includes Reporters without Borders and the federations of German startups and digital companies.