PARIS — Tomislav Nikolic, a nationalist and former cemetery supervisor, was elected president of Serbia on Sunday, in a surprise victory that cast doubt on whether the country would remain on its path toward the European Union or look increasingly eastward toward Russia.

His opponent, Boris Tadic, an ardent champion of Serbia’s effort to join the European Union, conceded defeat late Sunday. Mr. Tadic, who was president until he resigned last month to hasten an early election, had sought to portray a potential Nikolic presidency as a dangerous return to the past.

With 40.67 percent of the vote counted, the official electoral commission said Mr. Nikolic, an outspoken admirer of Russia who was once a close ally of the former Serbian strongman Slobodan Milosevic, won 50.21 percent of the vote compared with 46.77 percent for Mr. Tadic of the Democratic Party. Mr. Tadic, a telegenic former psychology professor, ran on a campaign advocating for Serbia’s European Union membership.

Declaring victory, Mr. Nikolic, 60, said he remained committed to Serbia’s European Union aspirations. “Serbia will not walk away from its path to the E.U.,” he said, promising to restore Serbia’s economic fortunes while fighting corruption. “These elections were not about whether Serbia will go to E.U., they were about solving problems that the Democratic Party has created in Serbia,” he added.