Barack Obama received a rock star welcome in Berlin as he appeared at a public debate with Chancellor Angela Merkel, whom he praised as one of his "favourite partners" during his presidency.
Security was tight in front of the German capital's iconic Brandenburg Gate, where former US leader Mr Obama and Mrs Merkel appeared on a podium before thousands of people attending a gathering marking the 500th anniversary of the Protestant Reformation.
Police helicopters patrolled the skies and snipers with balaclavas watched the scene from nearby rooftops.
After lauding Mrs Merkel as someone who had done "outstanding work", Mr Obama launched a defence 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, Mr 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, Mr Obama also spoke of the need to see development aid and diplomacy as 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.
Mrs Merkel, who hosted Mr Obama at the same spot four years ago, was due to travel to Brussels later for a meeting with leaders of fellow Nato member states, including Mr Trump.
The appearance with Mr Obama was criticised by some German opposition politicians as a publicity stunt ahead of September's general election, in which Mrs Merkel aims to win a fourth term.