The law, conceived in an attempt to tackle the illegal drugs trade, was approved by a vote of 16 to 13, with the ruling Broad Front Party united in its favour, reported Xinhua.
It now awaits the signature of President Jose Mujica, who wants the market to start operating next year.
Uruguayans will be allowed to grow, buy and smoke marijuana under the legislation. The government will license growers, sellers and consumers, and those who buy from licensed pharmacies will be registered on a confidential database to enforce a maximum purchase of 40 grams a month.
While licensed individual consumers will be able to cultivate up to six plants at a time in their home and registered clubs will be able to grow up to 99, carrying, growing or selling the drug without a license could result in a prison term.
Although polls show up to two-thirds of Uruguayans oppose a government-run marijuana industry, Mujica says the global anti-drug war is costly and a failure, and believes bureaucrats can do a better job of containing addiction and beating organised crimes than police and soldiers.
The law was approved by Uruguay's lower house of Congress in July, despite criticism from some claiming that the government is underestimating the risks of marijuana.