Justice Albie Sachs said the common law definition of marriage is not consistent with the constitution in that gay couples do not have the same status and benefits of heterosexual couples.
"The declaration of invalidity is suspended for 12 months from the date of this judgement to allow parliament to correct the defect," he said in a ruling delivered at the Constitutional Court.
This decision comes after a year after the Supreme Court of Appeal said same-sex couples should be able to get married, and called for courts to change the common-law definition of marriage from being a "union between a man and a woman" to a "union between two persons".
Justice Sachs said the constitution represents a past based on intolerance and exclusion.
"It is clear that the exclusion of same-sex couples from... marriage, constitutes a denial to them of their right to equal protection and benefit of the law," he said.
The court ruled that parliament is "well-suited" to make the necessary changes, and has ordered that the common-law definition be changed, and that the Marriage Act be amended to add the word "spouse" rather than just referring to husband and wife.
He said the changes will happen automatically if parliament fails to take action within a year.
Recognition of same-sex marriages would put South Africa in the same league as Canada, the Netherlands, Belgium and Spain, which have enacted legislation granting gay and lesbian couples the same rights as heterosexual ones.
Britain is expected to legalise gay marriage before the end of the year.
A number of European countries -- Denmark, France, Iceland, Norway and Sweden -- offer a similar legal status to same-sex marriages as civil unions.
However in Africa, homophobia remains rife and homosexual relationships are taboo in many countries.
Gay unions are outlawed in some nations such as Nigeria and Egypt, and two Nigerians are currently facing the death penalty if convicted of sodomy.
The court also said religious groups opposed to same-sex marriages would not be affected by the judgement.
The hallmark of an open and democratic society is its capacity to accommodate and manage difference of intensely-held world views and lifestyles in a reasonable and fair manner," said the judge.
Gay and lesbian rights organisation Out in Pretoria welcomed the judgement, but said it was disappointed that they would have to wait another year before the legislation is enacted.
