The head of the Zurich section of the rightwing Swiss People's Party, Ueli Maurer, has been voted by parliament into the seven-member government.
Maurer, who will take over the defence portfolio from the outgoing minister, Samuel Schmid, received 122 out of 243 valid votes in a third ballot. The absolute majority was 122 votes.
The voting was extremely close with party colleague Hansjörg Walter, head of the Swiss Farmers' Association, receiving 121 votes. He had earlier said he was not a candidate for the cabinet position.
In a first round of voting, Walter received 109 votes, with Maurer receiving 67. Former Justice Minister Christoph Blocher, who was on a double ticket with Maurer, withdrew his candidature after receiving only 54 votes.
At the same time, the Green Party's candidate Luc Recordon also pulled out of the race after receiving none of the 241 valid votes.
In the second round of voting Walter received 121 votes (122 were needed for an absolute majority) and Maurer 119.
After the vote, 58-year-old Maurer thanked parliamentarians, at first in French and then in German, for their support, saying his election was a "yes" for consensus.
He commented that he was happy for the People's Party that it was now in government again.
A year ago, Blocher was voted out of the cabinet and Schmid, snubbed by the People's Party, left to join the new Conservative Democratic Party.
Maurer reassured parliamentarians that he knew what his new position involved and what was expected from him.
He said he had received the mandate to work in a collegial system to find solutions for this "wonderful country" and all its residents.
People's Party President Toni Brunner said that although the party had returned to the government, it was still under-represented.
He called for a second seat with a candidate "who should also be chosen by the party".
The president of the centre-right Christian Democratic Party, Christophe Darbellay, expressed his "confidence" that Maurer would to be able to work with the other political forces in the cabinet.
The head of the parliamentary group of the centre-right Radical Party, Gabi Huber, said she was satisfied with the result.
She added that her party now expected Maurer to stick to the principle of collegiality "which he assured us in a credible way at a hearing that he would".
Radical Party head Fulvio Pelli told Swiss television that parliament had given a clear signal with the election that it wanted the People's Party back in the government.
The president of the Green Party, Ueli Leuenberger was not convinced. He expressed disappointment in the election, adding that the one-vote difference sent a clear signal of mistrust.
Christian Levrat, president of the centre-left Social Democrats, said the election marked the political end for Blocher. He said, with some satisfaction, that the former minister had "disappeared from the political scene for good".
He added that those who had elected Maurer, in particular the Radicals and Christian Democrats, would now have to take responsibility for supporting him.
swissinfo with agencies
Schmid bows out
In his farewell speech before Switzerland's parliament, Defence Minister Samuel Schmid again called for more civility in politics.
Schmid did not explicitly mention the Swiss People's Party, in which he had for years served as a moderate before leaving for the newly formed Conservative Democratic Party.
He nevertheless devoted much of his address to the rightwing populist political machine.
"Let us not always speak about our differences," the minister from canton Bern said.
Schmid said that while politics is characterised by conflict, polemics and polarisation did not belong in the country's political culture.
Swiss political values, he said included "freedom of thought and action, respect and decency towards others".
Wednesday's address echoed the November speech in which the defence minister announced his resignation.
Schmid said he had tried his best to address the country's political challenges and that he was satisfied with the new security policy he had championed.