Speaking at an open-air mass in the capital Budapest, the pontiff said it was "sad and painful" to see doors closed to people who were "unlike us".
Up to 100,000 people attended the mass, including Prime Minister Viktor Orban.
Mr Orban has criticised migration, building a barbed-wire border fence to stop migrants crossing into Hungary.
Addressing the crowd near the Hungarian parliament building on Sunday, Pope Francis pleaded to everyone - including "those with political and social responsibilities" - to be more open.
He was speaking about "the closed doors of our selfishness with regard to others... the doors we close towards those who are foreign or unlike us, towards migrants or the poor".
During the mass, the Argentinian pontiff, 86, also referred to Russia's full-scale invasion of Ukraine launched in February 2022.
He prayed for the "beleaguered Ukrainian people and the Russian people" and for "a future of hope, not war".
Later, on his flight home, he said the Vatican was involved in a peace mission to try to end the war. "There is a mission in course now but it is not yet public. When it is public, I will reveal it," he told reporters.
He also said the Vatican was ready to help facilitate the return of Ukrainian children taken to Russia since the invasion. Kyiv says thousands of Ukrainian children have been deported to Russia and calls it a war crime.
The Pope said he had discussed the Ukraine conflict with Mr Orban and a representative of the Russian Orthodox Church.
The three-day visit was Pope Francis' first major trip to Hungary since he became Pope 10 years ago.
What kept him away was the tough anti-migrant stance of Mr Orban, in contrast to his own compassion for all refugees, says the BBC's Nick Thorpe in Budapest.
What brought him to the country, besides his support for Catholics, was the war in Ukraine, our correspondent adds.
Hungary and Ukraine share a 134km (85-mile) border.
Unlike other leaders of the European Union, Mr Orban has refused to back military aid for Ukraine and maintained relations with Russian President Vladimir Putin.
During the carefully calibrated visit to Hungary, the Pope met people of all spheres of society, including youths, the poor, politicians and academics, our correspondent says.