Social media companies in Germany face fines of up to 50m euros ($57.1; £43.9m) if they fail to remove “obviously illegal” content in time.
From October, Facebook, YouTube, and other sites with more that two million users in Germany must take down posts containing hate speech or other criminal material within 24 hours.
Content that is not obviously unlawful must be assessed within seven days.
The new law is one of the toughest of its kind in the world.
Failure to comply will result in a 5m euro penalty, which could rise to 50m euros depending on the severity of the offence.
In a statement, Facebook said it shared the goal of the German government to fight hate speech.
It added: “We believe the best solutions will be found when government, civil society and industry work together and that this law as it stands now will not improve efforts to tackle this important societal problem.”
German MPs voted in favour of the Netzwerkdurchsetzungsgesetz (NetzDG) law after months of deliberation, on the last legislative day before the Bundestag’s summer break.
But it has already been condemned by human rights groups and industry representatives.
They claim the tight time limits are unrealistic, and will lead to accidental censorship as technology companies err on the side of caution and delete ambiguous posts to avoid paying penalties.
The law will not come into force until after the German federal elections, which will be held in September.
Justice Minister Heiko Maas singled out Facebook, which has some 30 million users in Germany, saying experience had shown that without political pressure, “the large platform operators would not fulfil their obligations” to take down illegal content.
He added that while the law “does not solve all problems”, it tackles the issue of hate crimes on social media, which are “increasingly a problem in many countries”.
Mr Maas, who oversaw the legislation, told the German parliament that online hate crimes had increased by almost 300% in the past few years, adding that “no one should be above the law”.
The bill was drafted after several high-profile incidents of fake news and criminal hate speech being spread on social media sites in Germany.
One case involved the targeting of prominent Green MP Renate Kunast, with a post that falsely suggested she was sympathetic to a refugee who had murdered a German student in the southern city of Freiburg.
For its part, Facebook said it had already made “substantial progress” in removing illegal content, and called into question the efficacy of the law.
The company recently announced it had hired an extra 3,000 staff (on top of the 4,500 it already has) to help monitor “the millions of reports” that come through every week.
Social media companies also point to a recent report by the European Commission, which showed that some 80% of all reported illegal content is already removed in Germany.