Image copyright Getty Images Image caption Counting went on all night at SPD headquarters in Berlin

German Chancellor Angela Merkel is set to form her fourth government after the opposition Social Democrats (SPD) voted in favour of another grand coalition.

The vote by 464,000 rank-and-file members ends five months of political deadlock since September's election.

The SPD had been split between the party's leadership, which backed joining the coalition, and its radical youth wing, which did not.

Mrs Merkel, who has been in power for 12 years, congratulated the SPD.

On her party's Twitter feed, she said she "looks forward to working together again for the benefit of our country".

SPD voters approved continuing the coalition with 66% in favour. The party leadership had been concerned younger voters might veto a deal. Vote counting went on through the night at the party headquarters in Berlin.

Interim SPD leader Olaf Scholz declared: "Now we have clarity. The SPD will enter the next government".

The chancellor now faces a range of challenges, including strong opposition from the nationalist Alternative for Germany (AfD).

The anti-immigrant party entered the federal parliament for the first time in September with just over 12% of the vote and now makes up the largest opposition group.

Image copyright AFP Image caption SPD members applaud as the result is announced

The SPD suffered its worst ever election result and many blamed their coalition with Mrs Merkel's Christian Democrats (CDU/CSU) for that poor performance.

Mrs Merkel, who also did badly, losing 65 seats, had tried and failed to form an alliance with the liberal Free Democrats (FDP) and the Greens.

She was forced to pay a higher price for continuing the existing coalition - the new finance minister will be a Social Democrat.

The SPD will now decide who will fill the six ministerial roles it is entitled to before Mrs Merkel's expected election by parliament on 14 March.

Martin Schulz, the former high-profile SPD chairman, had put himself forward as a possible foreign minister, but dropped the plan amid internal party squabbles over who should fill the post.

He had also annoyed many party members by first ruling out a coalition in the aftermath of the SPD's poor election showing only to enter talks later.

The end to uncertainty, including the possibility of fresh elections, will be greeted with relief in the European Union's institutions, where Germany, Europe's largest economy, is a major influence.

Merkel has her work cut out

Analysis by Jenny Hill, BBC News, Berlin

Germany has waited nearly six months for this. Its new government is a continuation of the last one - a coalition between Angela Merkel's conservatives and the Social Democrats, or SPD.

Mrs Merkel will no doubt be relieved. But this is no glorious victory. The SPD - which had the final say on the coalition deal - has torn itself apart over the decision to renew the alliance. Many members still oppose it and the internal schism has worsened what were already dismal approval ratings.

There's also limited public enthusiasm for the union. Months of political wrangling (don't forget Mrs Merkel tried - and failed - to form a coalition with the Green and FDP parties) have done little to inspire confidence in Germany's established parties.

Angela Merkel, herself profoundly damaged by the whole episode, has her work cut out now if she's to deliver the stability she promised her country.