German Chancellor Angela Merkel | Carsten Koall/Getty Images SPD grabs key ministries as Merkel clinches coalition deal Social Democrats to take charge of foreign affairs and finance but their members must give approval.

BERLIN — Germany's leading parties agreed a deal for a new government on Wednesday, setting the stage for a fourth term for conservative Chancellor Angela Merkel but giving Social Democrats control over the finance and foreign ministries.

After an all-night negotiating session, officials from Merkel's center-right bloc and the center-left SPD confirmed a deal had been reached. No details were officially released but national media outlets reported that SPD leader Martin Schulz would become foreign minister, while Hamburg mayor Olaf Scholz would take the finance minister post.

Schulz plans to hand over the party leadership to parliamentary group chief Andrea Nahles to focus on the foreign ministry if the deal is approved by SPD party members, Süddeutsche Zeitung and Spiegel reported.

Although Merkel will remain the leading force in German European policy, the SPD's victory in securing the finance and foreign ministries will give the party a big say in setting the future direction of the eurozone and the European Union.

The SPD will also take the labor, family, justice and environment ministries, news agency DPA reported.

Horst Seehofer, the leader of the Bavarian sister party of Merkel’s Christian Democratic Union, is set to become interior minister. Merkel’s own party will take the defense, economics, agriculture, health and education ministries, Spiegel reported.

Before a government can be formed, the deal must be approved by a vote of the SPD membership.

"We have a coalition agreement, which is positive for a great many of our citizens," said Peter Altmaier, Merkel's chief of staff and the acting finance minister. "Now we all want to go and shower because we negotiated long and hard in the final hours."

The parties still had some minor issues to iron out, broadcaster ARD reported. “But basically everything is settled,” ARD quoted an insider as saying.

The SPD used a WhatsApp message to confirm a deal had been agreed. "Tired. But satisfied. The agreement has been reached! Now the final details will be added to the text."

The deal must still be approved by various party bodies but that is expected to be a formality. The biggest remaining hurdle is the SPD's membership vote, which may take a couple of weeks to organize.

The coalition talks, meant to put an end to Germany's unprecedented four-month political deadlock, were scheduled to conclude Sunday but were stalled by controversies over the country's two-tier health care system and labor rules.

On the former, the parties found a compromise in setting up a commission to discuss how to adjust payments made to doctors treating patients covered by public and private health insurance, DPA reported.

On the second crunch issue, the rights of temporary workers, the parties reportedly agreed on cutting the maximum length of temporary work contracts from two years to 18 months.

Germany has been without a new government since last September’s general election, which left Merkel’s CDU/CSU alliance in first place but needing to form a coalition to get a governing majority. The veteran chancellor first tried to form a government with the liberal Free Democrats and environmentalist Greens but those talks collapsed in November.

The SPD, Merkel’s junior partner in the last government, declared after the election that it would not form a new “grand coalition” with the conservatives. But Schulz changed his stance after the failure of Merkel’s talks with the liberals and the Greens.

This article has been updated.