Former President Barack Obama joined German Chancellor Angela Merkel in Berlin on Thursday to mount a defense of Western liberalism at a time when Europe and the U.S. have experienced a tide of right-wing nationalism.

The occasion marked Obama's first public trip abroad since leaving office, and was set against the backdrop of President Donald Trump's first NATO summit in Brussels . Merkel will travel there to participate in the meetings as well.

Obama and the German leader spoke before a crowd of thousands in front of the iconic Brandenburg Gate, and though it recalled his blockbuster speech at the same spot as a candidate in 2008, tight security reflected heightened tensions following yet another terrorist attack, this one on Monday in Manchester , England.

While Obama avoided outright mentioning Trump, he made pointed remarks that could be seen as criticizing his successor's policies, such as efforts to temporarily close U.S. borders to refugees and people traveling from multiple Muslim-majority nations.

"We have to push back against those trends that would violate human rights or that would suppress democracy or that would restrict individual freedoms of conscience and religion," Obama said.

"In the eyes of God, a child on the other side of the border is no less worthy of love and compassion than my own child," he said. "We can't distinguish between them in terms of their worth and their inherent dignity, and that they're deserving of shelter and love and education and opportunity."

"Part of the job, I think, of governments is to express humanity and compassion and solidarity with those in need, but also recognize that we have to operate within legal constraints and institutional constraints and the obligations that we have to the citizens of the countries that we serve," he said. "And that's not always easy."

Obama stressed that part of the challenge he faced as president was "getting people to understand" that things like development aid and conflict resolution are necessary in a globalized environment.

"In this new world that we live in, we can't isolate ourselves," he stated. "We can't hide behind a wall."

The former president also defended his signature legislative achievement, the Affordable Care Act, amid Republican lawmakers' renewed efforts to dismantle it. On Wednesday, the nonpartisan Congressional Budget Office said the GOP's plan to gut the law known as Obamacare would leave 23 million fewer people with insurance and cause premiums for some to soar.

"My hope was that I was able to get 100 percent of people health care while I was president. We didn't quite achieve that, but we were able to get 20 million people health care who didn't have it before," Obama said.

Photos: Trump's First Foreign Trip View All 44 Images

"Certainly, I have some regrets," he said of not achieving fully universal health coverage in America.

"Obviously some of the progress that we made is now imperiled ... but the point though is that for those 20 million people, their lives have been better," Obama said. "We have set a standard of what's possible that people can then build on."

The two leaders additionally said that economic policies should promote greater opportunity for everyone.

"We will only be able to live in a good and peaceful society if everyone is doing well. That is what we mean when we say inclusive growth and we have to understand this and invest in this," Merkel said, according to CNN.

"One of the major questions that this generation and future generations will have to face is the growing gap in opportunity and increasing inequality that we are seeing between nations and within nations," Obama said.

Obama has been highly popular in Germany, though according to The Associated Press , his appearance was criticized by some in the German opposition as a publicity stunt ahead of elections this fall in which Merkel, leader of the Christian Democrats, faces a stiff challenge from the center-left Social Democrats' Martin Schulz.