Under the scheme, addicts visit clinics up to twice a day, where they inject the drug under medical supervision. They can also be treated for other medical issues or mental health problems, out correspondent says.
The policy is described as one of last resort - prescribing addicts with the very drug that caused their problems in the first place - but supporters say it works, and Swiss voters appear to have agreed, the BBC's Imogen Foulkes in Berne says.
Switzerland will be the first country to include it in government policy.
Supporters say it has had positive results - getting long-term addicts out of Switzerland's once notorious "needle parks" and reducing drug-related crime.
Opponents say heroin prescription sends the wrong message to young people and harms the addicts themselves.
On the cannabis issue, the government had opposed a change to the law.
Swiss police regularly turn a blind eye to moderate cannabis use.
But recent studies suggesting that long-term use of the drug may be more harmful than previously thought had looked likely to encourage a "No" to decriminalisation.
Jo Lang, a Green Party MP from Zug, said he was disappointed that the proposal to change the law on cannabis had failed.
"People have died from alcohol and heroin, but not from cannabis," said Mr Lang.
This page is best viewed in an up-to-date web browser with style sheets (CSS) enabled. While you will be able to view the content of this page in your current browser, you will not be able to get the full visual experience. Please consider upgrading your browser software or enabling style sheets (CSS) if you are able to do so.