Speaking at a campaign rally in Miami for Democratic nominee Hillary Clinton, the president said Trump's comments undermined American democracy.
Trump refused in a televised debate to say he would accept the outcome of the election on 8 November.
He later said he would accept a "clear" result but left a challenge open.
Speaking in Ohio on Thursday, Trump said, with a grin: "I would like to promise and pledge to all of my voters and supporters and to all of the people of the United States, that I will totally accept the results of this great and historic presidential election - if I win."
In the same speech, he said he would accept a clear election result but reserved the right to file a legal challenge in the case of a questionable one.
Hours later, the president said that sowing the seeds of doubt in people's minds about the legitimacy of US elections provided a boost to the country's enemies.
"You are doing the work of our adversaries for them, because our democracy depends on people knowing that their vote matters," said Obama.
Trump has been heavily criticised by many in his own party by suggesting he might not accept the election result.
For days, he has claimed the election is rigged against him, due to media bias and voter fraud.
During Wednesday night's debate with Hillary Clinton, when moderator Chris Wallace asked Trump if he would accept losing to her, the Republican nominee said he would "keep you in suspense".
Trump's campaign manager, Kellyanne Conway, later insisted that the candidate had meant he would not concede until the "results are actually known".
Republican Senator John McCain, who lost to Obama eight years ago, said: "A concession is not just an exercise in graciousness. It is an act of respect for the will of the American people, a respect that is every American leader's first responsibility."
First Lady Michelle Obama also joined the attack on Thursday, saying "you do not keep American democracy in suspense".
Hillary Clinton walked on to her campaign plane to the cheering and clapping of her aides.
She told reporters she was relieved and grateful and joked there would be "no more naps"- a reference to Trump's repeated description of her prep days off the campaign trail as naps.
Hillary's stand-in for Trump during the mock debates was one of her close aides, Philippe Reines, who took the role so seriously that he wore Trump cufflinks, shoe lifts and the same red tie as Trump. After the debate, she and Reines embraced and he called her a "badass hombre".
Hillary aides said she would continue to highlight Trump's refusal to pledge he would accept the results of the election. But would it be a real crisis on election day? Not if the result was a decisive win, they seemed to quietly indicate.
If Hillary and her team felt that she had closed the deal on stage, they kept their confidence in check. But the mood on the plane was certainly relaxed.
At the Ohio rally, Trump also reiterated a claim he made during the debate, that Hillary and President Obama were responsible for inciting violence at a Chicago rally earlier this year.
The crowd erupted into cheers of: "Lock her up!"
During the debate, he called Hillary a "nasty woman".
Trump has trailed Hillary in the polls after facing damaging fallout over a video that emerged of him making obscene remarks about groping women.
When asked to address the allegations made against him by several women in the wake of the video, Trump said the claims had been "largely debunked".
Donald Trump's comments come after a 10th woman came forward to accuse him of sexual assault on Thursday at a news conference.
Karena Virginia said Trump allegedly touched her breast at the US Open in 1998 and made offensive comments about her to a group of men.
The two candidates are scheduled to appear at a charity dinner on Thursday night in New York.
Polls suggest Hillary is ahead nationally and in key battleground states.