Hardline nationalist Prime Minister Viktor Orban, who faces an election next year, has grown increasingly radical on social policy, railing against LGBT people and immigrants in his self-styled illiberal regime, which has deeply divided Hungarians.
His Fidesz party, which promotes a Christian-conservative agenda, tacked the proposal banning school talks on LGBT issues to a separate, widely backed bill that strictly penalizes pedophilia, making it much harder for opponents to vote against it.
The move, which critics say wrongly conflates pedophilia with LGBT issues, triggered a mass rally outside parliament on Monday, while several rights groups have called on Fidesz to withdraw the bill.
Fidesz lawmakers overwhelmingly backed the legislation on Tuesday, while leftist opposition parties boycotted the vote.
Under amendments submitted to the bill last week, under-18s cannot be shown any content that encourages gender change or homosexuality. This also applies to advertisements. The law sets up a list of organizations allowed to provide education about sex in schools.
Gay marriage is not recognized in Hungary and only heterosexual couples can legally adopt children. Orban's government has redefined marriage as the union between one man and one woman in the constitution, and limited gay adoption.
Critics have drawn a parallel between the new legislation and Russia's 2013 law that bans disseminating "propaganda on non-traditional sexual relations" among young Russians.
Poland's conservative ruling party Law and Justice (PiS), Fidesz's main ally in the European Union, has taken a similarly critical stance on LGBT issues. Budapest and Warsaw are at odds with the European Union over some of their conservative reforms.
The European Parliament's rapporteur on the situation in Hungary, Greens lawmaker Gwendoline Delbos-Corfield, slammed the new law on Tuesday: "Using child protection as an excuse to target LGBTIQ people is damaging to all children in Hungary."
Orban has won three successive election landslides since 2010, but opposition parties have now combined forces for the first time and caught up with Fidesz in opinion polls.