BRUSSELS—The European Union agreed by a strong majority Tuesday on a mandatory plan to relocate 120,000 asylum seekers across the continent over the next two years, the centerpiece of its efforts to better respond to the biggest influx of migrants since after the end of World War II.

Four governments—Hungary, the Czech Republic, Slovakia and Romania—opposed the proposal, possibly setting the stage for intensified friction within the bloc over the contentious issue. Finland abstained.

“We would have preferred to have adoption by consensus but we were not able to achieve that. And it is not for want of trying I dare say,” said Jean Asselborn, foreign and interior minister from Luxembourg, which currently holds the rotating presidency of the EU.

He said that despite the divisions, the bloc would have been more damaged if it hadn’t been able to sign off the relocation proposal on Tuesday.

“If we had not done this, Europe would have been even more divided and its credibility even more undermined,” he said.