Incoming Prime Minister Jose Luis Rodriguez Zapatero announced the policy change on Thursday.
Zapatero told parliament that "the moment has finally arrived to end … the intolerable discrimination which many Spaniards suffer because of their sexual preferences".
"Homosexuals and transsexuals deserve the same public consideration as heterosexuals," he said.
"As a result we will modify the Civil Code to recognise their equal right to marriage with the resulting effects over inheritance, labour rights and social security protection."
The move is likely to stir controversy in one of Europe's most Catholic countries as the Vatican condemns same-sex unions and homosexuality was banned under Spanish dictator Francisco Franco.
The pope has consistently
condemned gay marriage
Spain's Catholic bishops have already spoken out strongly against the adoption of children by homosexual couples.
Homosexuality was banned during Franco's 1939-1975 dictatorship.
Spain's liberal 1978 constitution outlawed sexual discrimination and homosexuality was legalised shortly
Nine other European Union countries already have some provision for recognising those in committed same-sex relationships.
Last month, Britain said it would give legal recognition to gay partnerships.
In the United States, the fight over gay marriage has become an election-year issue after officials in several states used ambiguities in the law to begin marrying gay couples.
President George Bush is backing an amendment to the US Constitution to ban same-sex marriages.
Zapatero, whose Socialist party swept to a surprise victory in general elections last month, made legalising gay unions one of his campaign pledges.