With former President Barack Obama nipping at his heels in Europe, President Trump neared the end of his first foreign trip Thursday by largely fulfilling a transformative agenda that was more ambitious than anything Mr. Obama tried overseas during his first year in office.

On his nine-day trip, Mr. Trump has hit most, if not all, of the notes he wanted to on a broad range of issues: defeating the Islamic State, isolating Iran, linking symbolically three of the world’s great religions against terrorism, challenging the Muslim world to shut off the financing of extremism, promoting Middle East peace talks and confronting Western allies to get serious about their own security.

But there was Mr. Obama, still a darling of Europe, demanding attention Thursday with a public appearance in Berlin while Mr. Trump was meeting with European leaders just across the border at NATO headquarters in Belgium.

Mr. Obama even took a veiled swipe at Mr. Trump, telling an audience of thousands at an outdoor venue that modern nations “can’t hide behind a wall.”

“In this new world we live in, we can’t isolate ourselves. We can’t hide behind a wall,” Mr. Obama said to cheers at a forum with German Chancellor Angela Merkel.

Mr. Obama made the comment as Mr. Trump, who plans to build a wall along the U.S. border with Mexico, was meeting NATO and other European leaders in Brussels. Ms. Merkel, whom Mr. Obama praised as his “favorite partner” while in office, later met with Mr. Trump and got an earful from the new president about Europe’s negligent immigration policies.

Mr. Obama attended the Berlin event, sponsored by the Protestant Church, at Ms. Merkel’s invitation. He also warned the audience of young people to be wary of leaders who don’t question themselves.

“If I become so convinced that ‘I’m always right,’” Mr. Obama said, “the logical conclusion of that often ends up being great cruelty and great violence.”

Mr. Obama received a rock-star welcome as he appeared with Ms. Merkel at the historic Brandenburg Gate. Thousands of people attended the gathering under heavy security to mark the 500th anniversary of the Protestant Reformation.

Some German opposition politicians criticized the Obama event as a publicity stunt to help Ms. Merkel in her bid to win a fourth term in September’s election.

The event had been planned since last year and ended up coinciding with Mr. Trump’s first international trip — a demanding itinerary taking him to Saudi Arabia, Israel, the West Bank, Vatican City and Italy, Belgium and Italy again.

Mr. Obama made 11 foreign trips during his first year in office. The longest was a seven-day excursion in November 2009 to Japan, China, South Korea and Singapore. Perhaps the most memorable trip during Mr. Obama’s first year in office was a visit to Cairo, where he gave a speech on “a new beginning” with the Muslim world and called the lack of a Palestinian state intolerable.

Mr. Trump went to Riyadh, Saudi Arabia, for a meeting with some 50 Arab and Muslim leaders to urge them to do more to confront extremism. He got an agreement on that front, then met with the leaders of Israel and the Palestinian Authority to encourage restarting peace negotiations.

Mr. Obama also visited Saudi Arabia during his first year in office but, unlike Mr. Trump, he skipped Israel. That decision “started him off on the wrong foot with Israeli Prime Minister [Benjamin] Netanyahu,” said Jim Phillips, an analyst on the Middle East at the conservative Heritage Foundation.

“President Trump had much better personal chemistry with Netanyahu and with the Arab leaders he met on the trip,” Mr. Phillips said. “In contrast to President Obama, who spoke to people over the heads of leaders in his 2009 Cairo speech, Trump focused directly on leaders. And he embraced Egypt’s President [Abdel-Fattah] el-Sissi, unlike Obama, who distanced himself from Egypt’s President [Hosni] Mubarak and embraced the leaders of the Muslim Brotherhood, who he mistakenly believed to be committed to democracy.”

He said Mr. Trump’s speech to Muslim leaders was effective on two of the administration’s highest priorities: confronting terrorism and sharing the burdens more fairly.

“His speech also made it clear that the U.S. is fighting an ideology — Islamist extremism — and not the religion of Islam, which is important from the standpoint of mobilizing Sunni Muslim leaders against ISIS and al Qaeda,” Mr. Phillips said, using an acronym for the Islamic State group.

Despite gleeful predictions from Mr. Trump’s liberal critics, the president has made no major gaffes on the trip. Probably the most glaring misstep came from an anonymous aide who described the president to the media as “exhausted” after his speech in Saudi Arabia.

Whether because of the president’s weariness or an overly ambitious itinerary, the White House did cut out a few events from Mr. Trump’s schedule at the start of the trip.

Otherwise, one of the few distractions was buzz on social media Thursday over a short video clip showing Mr. Trump pushing past another NATO head of state to gain a more prominent position in a gathering of members.

Alberto Mingardi, director general of the Bruno Leoni Institute, a libertarian think tank in Milan, said Mr. Trump’s visit to Italy was viewed as “pretty regular.”

“It well may be because we have all been busy in thinking about the terrible [terrorist attack] in Manchester, but I don’t think President Trump’s first visit in Italy made a big impression on people,” Mr. Mingardi said. “He did not say anything that outraged people, as even those less friendly toward the president should admit. All in all, I think the visit was not really different than the ones of his predecessors.”

As his trip nears its conclusion on Saturday, Mr. Trump said he believes he has made progress toward his goals of security and prosperity for Americans.

“My travels and meetings have given me renewed hope that nations of many faiths can unite to defeat terrorism, a common threat to all of humanity,” the president said.