By KIRSTEN GRIESHABER and FRANK JORDANS, Associated Press

BERLIN (AP) — Barack Obama received a rock-star welcome in Berlin as he appeared at a public debate Thursday with Chancellor Angela Merkel, whom he praised as one of his "favorite partners" during his presidency.

Security was tight in front of the German capital's Brandenburg Gate, where the former U.S. president and Merkel appeared on a podium before tens of thousands of people attending a gathering marking the 500th anniversary of the Protestant Reformation. All visitors had their bags searched, police helicopters patrolled the skies and snipers with balaclavas watched the scene from nearby rooftops.

At the beginning of their discussion, Obama praised Merkel to the excited cheers of a crowd of young Protestants, who had traveled from all over Germany and beyond to attend the religious gathering.

"Not only do I love this city, but one of my favorite partners throughout my presidency is sitting next to me," Obama said with a big smile. Merkel smiled right back at him.

"Chancellor Merkel has done outstanding work, not just here in Germany, but around the world," Obama said.

After lauding Merkel, he launched a defense of his own presidency and the values of liberal democracy championed by both leaders.

Citing the rise of nationalism and xenophobia in parts of the world, Obama told the crowd that "we have to push back against those trends that would violate human rights or suppress democracy or restrict individual freedoms."

In a veiled reference to his successor Donald Trump, who has proposed to cut support for diplomacy and international aid by nearly a third, Obama said those programs are essential aspects of national security policy.

"We can't isolate ourselves. We can't hide behind a wall," he said, to cheers from the audience.

The former president and the chancellor, who grew up in a pastor's household in East Germany, chatted about their faith and called on the youth to commit to helping make the world a better place — while keeping in mind that changes don't come quickly from one day to the next.

Referring to the terror attack in England just days ago, Obama said that he and the chancellor were "heartbroken about the loss of life ... and that it's a reminder that there is great danger and terrorism and people who want to do great harm to others just because they're different."

Despite describing the world as "a very complicated place," Obama and Merkel still found time to joke with each other and made the crowd laugh several times. The harmony and ease between the two leaders was a stark contrast to Merkel's meeting with Trump in Washington in March, when the U.S. president appeared to refuse to shake Merkel's hand in front of reporters. Trump has said he didn't hear a reporter calling for a handshake.

Merkel, who hosted Obama at the same spot four years ago, was due to travel to Brussels later Thursday for a meeting with Trump and leader of other NATO-member states.