WASHINGTON — President Obama told Senator Bernie Sanders in an Oval Office meeting on Thursday to channel the energy of his presidential campaign’s millions of supporters behind Hillary Clinton, and said that Mr. Sanders would play a central role in shaping the Democratic agenda if he did.

Less than an hour and a half later, Mr. Obama, who had tried to remain neutral in the race between Mr. Sanders and Mrs. Clinton, formally endorsed her.

Moving swiftly to unite his party after a primary campaign that has left many of Mr. Sanders’s supporters bitter and disillusioned, Mr. Obama, according to his aides, tried to mollify the maverick senator while prodding him to reorient his efforts against Mrs. Clinton into a broader bid to help Democrats in November.

There was no hint after the meeting that Mr. Sanders intended to challenge Mrs. Clinton for the nomination at next month’s convention, but hours later at a rally in Washington he urged voters there to go to the polls in their primary on Tuesday and to keep pushing for a political revolution.