The embattled center-left party of Chancellor Olaf Scholz of Germany scored a hard-fought and narrow victory over the far-right ethnonationalist party in an election in an eastern state of the country, potentially energizing Scholz’s government in Berlin.