The vote was 16 to 13, with the governing Broad Front majority united in favour. The plan awaits the signature of president Jose Mujica who wants the market to begin operating next year.
Two-thirds of Uruguayans oppose a government-run marijuana industry, according to opinion polls, but Mr Mujica said he is convinced the global drug war is a failure and feels bureaucrats can do a better job of containing addictions and beating organised crime than police, soldiers and prison guards.
Senator Constanza Moreira, voting with the Broad Front majority, said: "Today is a historic day. Many countries of Latin America will take this law as an example."
Uruguay's drug control agency will have 120 days to draft regulations imposing state control over the entire market for marijuana, from seed to smoke.
Everyone involved must be licensed and registered, with government monitors enforcing limits such as the 40 grams a month any adult will be able to buy at pharmacies for any reason or the six marijuana plants that licence-holders will be allowed to grow at home.
Former health minister Alfredo Solari warned the effects of the policy on public health would be terrible.