The bill was approved at a Cabinet meeting and is expected to be presented to Parliament in February for debate.
"The right to marry is a right for everyone, without distinction. It cannot be understood as a privilege," Deputy Prime Minister Maria Teresa Fernandez de la Vega told a press conference after the Cabinet meeting. "The recognition of homosexuals' rights eradicates an unjustified discrimination."
Under the bill homosexuals will be allowed to adopt children and couples of the same sex will be able to inherit from one another as well as receive retirement benefits from their working spouses in the same way in which heterosexual married couples do now.
Since taking office in April, Prime Minister Jose Luis Rodriguez Zapatero has vowed to institute deep changes on social issues after eight years of conservative rule. On the day he was confirmed as prime minister, he vowed to Parliament to allow same-sex marriage and fight discrimination against homosexuals.
Zapatero runs a minority government but is generally supported by two small leftist parties. Gays could be able to start marrying as soon as next year.
Gay advocacy groups are supportive of the new legislation but the influential Catholic church adamantly disagrees. A spokesman of the Spanish Bishops Conference claimed some months ago that allowing same-sex marriages would be like releasing a virus into Spanish society.
If Parliament passes the legislation, Spain would join Belgium and the Netherlands in legalizing same-sex marriages.
Sweden and Denmark have "civil union" laws for same-sex couples, which fall short of legalizing same-sex marriage. However, in both countries the union can be blessed by the Lutheran Church, the state religion in Denmark and the dominant one in Sweden.