Pope Francis, who has made welcoming migrants, embracing minorities and warning against nationalism central tenets of his pontificate, visited Budapest for the second time in less than two years on Friday. The trip gives Prime Minister Viktor Orban, perhaps Europe’s chief opponent of migrants, closest ally of Russia and most vocal critic of gay rights, a political gift he is sure not to waste.

During a 10-year pontificate in which Francis has met with multiple strongmen and dictators to both protect his flock and safeguard human rights and peace, he has rarely confronted his hosts, looking instead for areas of agreement, even if it risks legitimizing policies he finds abhorrent.

Francis did not so much chastise Mr. Orban on Friday as indirectly implore the leader, who portrays himself as the defender of Christian values in Europe against foreign migrants and meddling from Brussels, to be a good Christian and a constructive participant in European unity.

Speaking to dignitaries in a former monastery in Budapest’s Castle District, Francis, making his 41st foreign visit and the first since being rushed to the hospital last month, talked about

how “Christian values cannot be proposed by rigidity and close-mindedness.” And he warned against a “certain tendency, at times proposed in the name of native traditions and even of the faith, to withdraw into oneself.”

He spoke against the fading of mature statesmanship toward the building of a united Europe while “nationalism is on the rise and ever harsher judgments and language are used in confronting others.” He bemoaned an “adolescent belligerence” and “self-referential forms of populism,” and spoke of the need to “welcome other peoples and to refuse to consider anyone an eternal enemy.”

But Francis, who called the visit an opportunity to “re-embrace” the Hungarian church, did not seem angry. On the contrary, he seemed in good spirits, happily eating the bread presented to him by children in traditional dress, and abandoning his wheelchair for a cane as he toured the Sandor Palace with President Katalin Novak, who spoke with him in Spanish.

The Vatican has said that the pope’s three-day apostolic visit to Hungarian Catholics, in which he will meet with Mr. Orban but also with refugees and the poor, is completely different than a 2021 stopover he made in Budapest for a few hours to celebrate Mass at the end of a weeklong Catholic congress.

Vatican officials argued that the pope’s remarks made it clear he would not be used as a pawn. But critics of Mr. Orban worry that no matter how noble Francis’ intentions may be, his trip is simply playing into Mr. Orban’s able political hands.

“In the history of the country, this is a really marvelous moment that you came to find us, your holiness,” Mr. Orban said as the two sat down together for a private meeting. He added that “this represents for us also a reconfirmation,” that the two had to maintain a common line on the Christian way, “which is very hard in this present Europe” and “also in this war that cries out for peace.”

Mr. Orban was especially delighted, Hungarian officials said, because he wasn’t sure the pope would come back.

After the 2021 visit, said Eduard Habsburg, the Hungarian ambassador to the Holy See, “we thought that was it!” Instead, Hungary is now pulling out the stops to welcome Francis again.

“I don’t know how the Vatican is reading this, or why they have decided to do it,” said Stefano Bottoni, a historian at the University of Florence who lives in Budapest. “But in Hungary, the pope’s visit has become an extraordinary showcase for the regime.”

Locals agreed that Mr. Orban would not let the opportunity pass.

“He will use this,” said Kristof Polgar, 25, who walked near St. Stephen’s Basilica’s in Budapest after a fencing class on Thursday. He said Francis was especially popular with the older generation of Catholics whom Mr. Orban relied on for political support, and that “Orban builds on it and he does it perfectly.”

In 2021, when Francis suggested he might not meet Mr. Orban on his way to a longer stay in Slovakia, Mr. Orban’s allies in the news media, where his party holds great sway, insulted Francis for slighting Hungary, for “behaving in an anti-Christian manner, and for “causing extraordinary damage to the Christian world.”

During that trip, Francis also indirectly sent Mr. Orban a message that God was not a strongman who muzzles foes, and that religious roots, while vital for a country, also allow it to open up and extend “its arms toward everyone.”

For years, Mr. Orban’s government has sought to blur the differences between Hungary and the Holy See by emphasizing their areas of agreement, including Mr. Orban’s establishment of a State Secretary for the Aid of Persecuted Christians and his defense of the traditional family. Mr. Habsburg, the ambassador, said the Vatican had even back-channeled requests for Hungary to be more publicly supportive of the Holy See and its views on human sexuality and gender roles in multilateral settings.

On Friday, Francis lamented the imposition of what he called “ideological colonization” that, he said “would cancel differences, as in the case of the so-called gender theory.”

But Hungarian officials argue that as much as the pope and Mr. Orban are divided on the issue of migration, they are in alignment when it comes to their desire for peace in Ukraine.

“I ask myself, thinking not least of war-torn Ukraine,” Francis said Friday, “where are creative efforts for peace? Where are they?”

In the early months of the war, Francis failed to call out Russia’s aggression.

But under criticism from Ukrainian leaders and with questions rising about his legacy, Francis spoke out more clearly against the invasion, saying in August that the Russian Federation had initiated a war that was “morally unjust, unacceptable, barbaric, senseless, repugnant and sacrilegious.”

Mr. Orban, on the other hand, has refused to supply Kyiv with weapons and threatened to veto European Union sanctions against Moscow. Hungary still receives much of its gas from Russia and it blocked efforts to sanction Patriarch Kirill of the Russian Orthodox Church, Mr. Putin’s religious patron and apologist, whom Francis once warned not to “transform himself into Putin’s altar boy.”

Yet Mr. Orban, increasingly isolated and eager to demonstrate a papal seal of approval, has sought to portray himself and the pope as on the same page because they have both called for cease-fires and peace negotiations. During a speech in February he argued that they were alone in Europe in calling for peace in Ukraine.

“So they seem to have the same idea,” said Mr. Habsburg, who called them “the only two voices in Europe that have said it that way.”

Analysts say this is simply Mr. Orban doing what he does best.

“Orban is the king of opportunists,” said Matteo Zola, a journalist and the editor of East Journal, an online newspaper focused on Central and Eastern Europe. “Hungary wants to show itself as the center around which one can imagine building a dialogue between Moscow and Europe or the West. And the pope’s trip legitimizes this role.”

But for Mr. Orban, he added, “it’s all capital to spend inside the country.”

Francis will meet on Saturday with some of the Ukrainian refugees who have remained in Hungary. When Mr. Orban visited the Vatican for his first official state visit last year, a development his government believed was key for Francis’ official visit, the pope thanked him for accepting the refugees.

“The issue of acceptance and welcome is a heated one in our time, and is surely complex,” Francis said, but he said Christians must welcome those “who flee in desperation from conflicts, poverty and climate change.” He added, “It is urgent then, as Europe, to work for secure and legal corridors and established processes for meeting an epochal challenge.”

But public opinion in Hungary, including among Catholics, is so supportive of Mr. Orban on the issue of migrants that even if Francis did throw down a gauntlet, analysts doubted it would matter.

“The weight of the things that he’ll say on migration” Mr. Bottoni, the historian, said “is zero.”