The larger of the two chambers of the federal parliament voted by 95 to 94, with three abstentions, in favour of quotas.
Opposition came from the conservative People's Party and Liberal-Radicals.
During a debate on the revision of company law on Thursday, the chamber agreed that in firms listed on the Swiss stock exchange and with more than 250 employees women must make up at least 30 percent of board members and at least 20 percent of the management, the Swiss news agency SDA reported.
However, there is no plan to impose sanctions on companies that do not fulfil the quotas.
Instead these firms will simply have to explain their reasons and set out how they plan to rectify the situation.
The new rules will come into force five years after the new law has been passed.
Speaking against the motion in parliament, Nathalie Rickli of the People's Party said that listed companies needed people with particular qualifications, regardless of their gender.
“I wonder if some women are not overestimating themselves,” she said.
Justice Minister Simonetta Sommaruga disagreed, saying many women had the right qualifications and were also doing great work.
But when it comes to power and money, women are largely absent, she said.
In the biggest Swiss companies eight out of ten board members are men and in management teams the figure is nine out of ten, according to the minister.
Around 200 firms would be affected, and no one would be forced to hire a woman or a man, she said.
Earlier this year a parliamentary commission recommended against imposing a quota on the number of women in the seven-member Swiss federal government.