The narrow win by Olaf Scholz’s party came in a state election, the third in a series that was seen as a reflection of the national mood and a snapshot of the government’s popularity.