After nearly 12 hours of debate, senators gave the government-sponsored bill their historic final approval.
The law allowing registered Uruguayans over 18 to buy up to 40g (1,4oz) of the drug a month is not expected to come into force before April.
The government hopes it will help tackle drug cartels, but critics say it will expose more people to drugs.
Dozens of supporters of the bill proposed by the left-wing President Jose Mujica gathered outside the Congress in Montevideo to follow the vote.
Presenting the bill to fellow Senators, Roberto Conde said it was an unavoidable response to reality, given that the "war" against drugs had failed.
"We have the duty as the state to give a specific answer to an open territory, small and non-producing," Mr Conde said, adding that Uruguay's borders are used by cartels to smuggle drugs into neighbouring countries.
'Experiment on people'
But many senators also spoke out against the bill, before it was passed by 16 votes to 13 on Tuesday.
The opposition member Alfredo Solari said Uruguay should not "experiment" on its people.
"This project envisages a social engineering experiment and respects none of the ethic safeguards of experimentation on human beings, and these are important in the case of a substance like marijuana, which causes damage to human beings," Senator Solari told Reuters news agency.
The project had already been approved by Uruguay's lower house in July.
It had also drawn international criticism. The International Narcotics Control Board (INCB) warned the law would "be in complete contravention to the provisions of the international drug treaties to which Uruguay is party".
The INCB is an independent body of experts established by the United Nations to monitor countries' compliance with international drug treaties.
The historic approval comes amid growing debate over drug legalisation in Latin America.
A group of former presidents and influential social figures, including Brazil's Fernando Henrique Cardoso, Mexico's Ernesto Zedillo and Colombian ex-leader Cesar Gaviria, have called for marijuana to be legalised and regulated.
But President Mujica recently asked during an interview why the former leaders only spoke out about the legalisation of marijuana after they had left office.
In July, without naming Uruguay directly, Pope Francis criticised drug legalisation plans during a visit to Brazil.