BUDAPEST — Lawmakers in Hungary on Tuesday rejected a proposed national ban on refugees relocated from the rest of the European Union, dealing a rare defeat to Prime Minister Viktor Orban.

Mr. Orban submitted a plan to ban the refugees, in the form of a constitutional amendment, last month, after a similar proposal failed to pass by referendum because of insufficient voter turnout.

He has vowed to block a European Union program that would resettle migrants from the Middle East and Africa who have gone to countries like Greece and Italy. Under that program, Hungary, a nation of 10 million, would have to accept 1,294 of a total of about 160,000 migrants.