(CNN) Angela Merkel was sworn in as Chancellor of Germany after lawmakers voted to re-elect her as leader in a close vote on Wednesday morning.

She will now begin a historic fourth term as Chancellor.

The vote in parliament ended almost six months of political turmoil after a federal election saw millions of voters desert the two mainstream parties -- Merkel's Christian Democratic Union and the Social Democratic Party (SPD) -- turning instead to parties on the left and right.

At 171 days, it is by far the longest period in modern German history between a federal election and the election of a Chancellor.

In a secret ballot, 364 of the Bundestag's 709 members voted in favor of Merkel -- nine more than the 50% required. Thirty-five MPs from the parties governing under Merkel did not vote to re-elect her.

Speaking on Monday in expectation of her re-election, Merkel said: "I think everyone has the feeling it's time to finally start working. A new departure for Europe, a new dynamic for Germany, new cohesion for our country -- that's what we have before us... So there is a lot of work ahead."

Merkel's victory marks the final stepping stone on the path to Germany's new government -- a renewal of the so-called grand coalition ("GroKo") between the Chancellor's CDU/Christian Social Union alliance and the SPD.

The former leader of the SPD, Martin Schulz, had initially ruled out a new GroKo, pledging to take his party into opposition, but was forced to change his stance after coalition talks between the CDU, Green Party and liberal Free Democratic Party collapsed in November, raising the possibility of new elections.

After weeks of negotiations, a coalition treaty was produced and later approved by SPD members who voted via postal ballot . But many SPD members and politicians remain unhappy with their party's involvement in the new coalition.

Government must 'win back lost trust'

In a speech nominating Merkel as Chancellor on Wednesday morning, President Frank-Walter Steinmeier warned that the new coalition must win back the trust of voters and protect Germany's liberal democracy, alluding to the rise of the far-right Alternative for Germany (AfD) without mentioning the party by name.

"In order to win back lost trust, a simple restoration of old ways will not suffice. This government has to prove itself in new and different ways."

"The liberal democracies of the West are beset with challenges -- from without as well as from within," he said. "Even in parts of Europe elections are being won with isolationism, nationalism and an unwillingness to compromise."

Chancellor Angela Merkel with German President Frank-Walter Steinmeier Wednesday

He urged Germany to set an example to the world by remaining democratic, outward-looking and peaceful, adding that the country is in a better position than many others to prove the value of liberal democracy.

"We have proven in the past that we can free ourselves from the most difficult situations with courage and determination," he said.

The renewal of the grand coalition means the AfD is now officially the lead opposition party in parliament.

Founded in 2013, the party ran on a virulently anti-immigration platform that proved popular with voters in the September election.

The party surged into third place in the election with 12.6% of the vote , the first time in decades that a far-right, openly nationalist party had won seats in Germany's federal parliament.

AfD politicians have campaigned strongly against Merkel and her policies. Following the election result in September, co-leader Alexander Gauland promised his party would provide staunch opposition to the new government, adding: "We will hunt them. We will hunt Mrs. Merkel or whomever. And we will take back our country and our people."

Protest was evident even as Merkel was being re-elected and sworn in. As she was taking her oath, a protester in the parliament's public gallery held up a poster reading "Merkel must go," a phrase popularized by the AfD.

AfD lawmaker Petr Bystron was also fined €1,000 ($1,240) for posting a picture of his ballot paper on Twitter with the comment "not my Chancellor."

Refugees and EU reform at top of coalition agenda

Priorities for the new government will include new policies on refugees and European Union reforms. The new government wants to cap the number of asylum seekers taken in each year by Germany at 180,000 to 200,000 and reinstate the right of refugees with temporary protection status to bring their families to Germany, limiting the number of new arrivals via that route to 1,000 per month.

The coalition has also pledged to work toward a Europe that can act independently and effectively on the global stage, while also strengthening European ties with the United States.

Regarding weapons exports, the parties have pledged to no longer approve exports to countries involved in the war in Yemen in what will be seen as a victory for German activists against the arms trade.