MONTREAL — Former President Barack Obama endorsed Justin Trudeau’s re-election as prime minister on Wednesday, potentially giving him a lift just days before Canadians go to the polls in closely fought elections.

“I was proud to work with Justin Trudeau as President,” Mr. Obama wrote on Twitter. “He’s a hard-working, effective leader who takes on big issues like climate change. The world needs his progressive leadership now, and I hope our neighbors to the north support him for another term.”

[Oct. 21 is Election Day in Canada. Here’s what you need to know.]

The approbation of Mr. Obama, who was very popular in Canada during his presidency, could buttress Mr. Trudeau at home, where his image as a liberal world leader — welcoming immigrants, defending the rights of women and Indigenous people, and fighting climate change and racism — has suffered repeated blows over the last year.

In September, he faced revelations that he dressed in brownface and blackface two decades ago. In August, Mr. Trudeau was also found to have violated an ethics law in his handling of a corporate criminal case, by improperly pressuring his attorney general to seek a civil penalty against a company accused of corruption, rather than a criminal conviction.