Former US president Barack Obama took aim at “strongman politics” in his highest-profile speech since leaving office, in what appeared to be a dig at his successor, Donald Trump.

Mr Obama urged people around the world to respect human rights and other values now under threat in an impassioned address marking the 100th anniversary of anti-apartheid leader Nelson Mandela’s birth.

Mr Obama’s speech to a cheering crowd of thousands in South Africa countered many of Mr Trump’s policies, rallying people around the world to keep alive the ideas that Mandela worked for including democracy, diversity and tolerance.

Mr Obama opened by calling today’s times “strange and uncertain”, adding that “each day’s news cycle is bringing more head-spinning and disturbing headlines”.

These days “we see much of the world threatening to return to a more dangerous, more brutal, way of doing business”, he said without mentioning Mr Trump by name.

He targeted politicians pushing “politics of fear, resentment, retrenchment”, saying they are on the move “at a pace unimaginable just a few years ago”.

Former US president Barack Obama delivers his speech (Themba Hadebe/AP)

He attacked “strongman politics”, saying that “those in power seek to undermine every institution … that gives democracy meaning”.

He spoke up for equality in all forms, saying that “I would have thought we had figured that out by now”, and he even invoked the World Cup-winning French team and its diversity.

He warned that countries that engage in xenophobia “eventually … find themselves consumed by civil war”.

And he noted the “utter loss of shame among political leaders when they’re caught in a lie and they just double down and lie some more”, warning that the denial of facts, such as that of climate change, could be the undoing of democracy.

People gather below a giant banner of the late Nelson Mandela (Themba Hadebe/AP)

But Mr Obama reminded the audience that “we’ve been through darker times. We’ve been through lower valleys”, and he closed with a call to action: “I say if people can learn to hate, they can be taught to love.”

He received a standing ovation.

This was Mr Obama’s first visit to Africa since leaving office in early 2017.

He stopped earlier this week in Kenya, where he visited the rural birthplace of his late father.

Mr Obama’s speech noted how Mandela, who was imprisoned for 27 years, kept up his campaign against what appeared to be insurmountable odds to end apartheid, South Africa’s harsh system of white minority rule.

Mandela, who was released from prison in 1990 and became South Africa’s first black president four years later, died in 2013, leaving a powerful legacy of reconciliation and diversity along with a resistance to inequality, economic and otherwise.

Until Tuesday, Mr Obama had shied away from public comment on Mr Trump, whose administration has reversed or attacked notable achievements of his predecessor.

Former US president Barack Obama, back right, with South African President Cyril Ramaphosa, back left (Themba Hadebe/AP)

The US under Mr Trump has withdrawn from the 2015 Paris climate agreement and the Iran nuclear deal while trying to undercut the Affordable Care Act or Obamacare.

Mr Obama’s speech drew on his great admiration for Mandela, a fellow Nobel Peace Prize winner whom America’s first black president saw as a mentor.

When Mr Obama was a US senator he had his picture taken with Mandela.

After Mr Obama became president he sent a copy of the photo to Mandela, who kept it in his office.

Mr Obama also made a point of visiting Mandela’s prison cell and gave a moving eulogy at Mandela’s memorial service in 2013, saying the South African leader’s life had inspired him.

Many South Africans view Mr Obama as a successor to Mandela because of his groundbreaking role and his support for racial equality in the US and around the world.