Pope Francis on Sunday presided over a big outdoor Mass where he urged Hungarians not to close the door on migrants and those who are “foreign or unlike us,” in contrast to the anti-immigrant policies of nationalist Prime Minister Viktor Orban.

More than 50,000 people gathered in and around the square behind Budapest’s iconic neo-gothic parliament building, a symbol of the capital on the Danube, to see the pope on the last day of his visit to the country.

He continued a theme he began on the first day of his visit on Friday, when he warned against the dangers of rising nationalism in Europe, but put it in gospel context, saying that closed doors were painful and contrary to the teachings of Jesus.

Orban, a populist who was attending the Mass, sees himself as a protector of Christian values. He has said he would not allow Hungary to be transformed into an “immigrant country,” as he claims others in Europe have become, unrecognizable to its native peoples.

In his homily, 86-year-old Francis said that if Hungarians wanted to follow Jesus, they had to shun “the closed doors of our individualism amid a society of growing isolation; the closed doors of our indifference towards the underprivileged and those who suffer; the doors we close towards those who are foreign or unlike us, towards migrants or the poor”.

Francis believes migrants fleeing poverty should be welcomed and integrated because they can culturally enrich host countries and boost Europe’s dwindling populations. He believes that while countries have a right to protect their borders, migrants should be distributed throughout the European Union.

Orban’s government has built a steel fence on the border with Serbia to keep out migrants.

In his homily, Francis also spoke against doors “closed to the world”.

Peter Szoke, leader of the Hungarian chapter of the Sant’ Egidio peace community, who attended the Mass, agreed with the pope’s prescription.

“There is great temptation to be self-referential, to refer everything only to ourselves, only to our own reality, whereas there are other realities too – the realities of the poor, the realities of other nations, the realities of wars, of injustices,” he said.

Sunday’s homily was the second time Francis has used a religious context to make his point. On Friday, he quoted what St Stephen, the 11th century founder of Christian Hungary, had written about welcoming strangers.

In his customary Sunday address to the crowd after the Mass, Francis mentioned the war in Ukraine, on Hungary s eastern border. He prayed to the Madonna to watch over both the Ukrainian and Russian people.

“Instil in the hearts of peoples and their leaders the desire to build peace and to give the younger generations a future of hope, not war, a future full of cradles not tombs, a world of brothers and sisters, not walls,” he said.

The three-day trip is the pope’s first since he was admitted to hospital for bronchitis in March.