The vote took place three weeks after the far right made gains in two other states in eastern Germany.
According to final results published on Sunday evening by the state electoral administration, the Social Democrats won 30.9 per cent of the votes in the election to the parliament of Brandenburg, the state that surrounds Berlin.
The far-right Alternative for Germany was a close second with 29.2 per cent.
The right-wing Alternative for Germany party is led by Bjoern Hoecke. (AP PHOTO)
A new leftist movement, the Sahra Wagenknecht Alliance, or BSW, came in third with 13.5 per cent while the centre-right Christian Democrats took 12.1 per cent.
The first-place showing for the Social Democrats brought a reprieve to the beleaguered Scholz, whose three-party governing coalition has fared poorly in elections so far this year.
The Social Democrats have governed Brandenburg continuously since German reunification in 1990, and a loss there would have been a major setback for Scholz, who has his constituency in the state capital, Potsdam.
Scholz has said he would like to be the party's candidate for chancellor in next federal election and Sunday's vote was also being watched for what it might signal about his political future.
"It's great that we won," Scholz said from New York, where he was attending a meeting at the United Nations, according to the German dpa news agency.
But the success of the Social Democrats in Brandenburg — after defeats elsewhere — was largely credited not to Scholz, but to the efforts of the popular state governor, Dietmar Woidke who said the strong showing of the far-right party means there was work to do.
"A lot of voters have voted for the AFD, and that's too much. So we have to think about these results and we have to make our policies better," Woidtke told The Associated Press.
The far-right party led by Bjoern Hoecke has gained support amid a growing backlash against large-scale migration to Germany over the past decade and recent extremist attacks. Germany's economy, once a powerhouse, has been weakening, adding to a general feeling of malaise.
Sunday's vote followed a heated election campaign centred on the issues of migration, internal security and peace.
Both the far right and the new leftist movement want to end weapons deliveries to Kyiv as Ukraine tries to defend itself against Russia's full-scale invasion.