The ruling by Chief U.S. District Judge Vaughn Walker touched off a celebration outside the courthouse. Gay couples waved rainbow and American flags and erupted with cheers in the city that has long been a magnet for gays.
Shelly Bailes embraced her wife Ellen Pontac as Bailes held a sign reading, "Life Feels Different When You're Married."
In New York City, about 150 people gathered in front of a lower Manhattan courthouse. They carried signs saying "Our Love Wins" as organizers read portions of the ruling.
Walker made his decision in a lawsuit filed by two gay couples who claimed the voter-approved ban violated their civil rights. The ruling "vindicates the rights of a minority of our citizens to be treated with decency and respect and equality in our system," said former U.S. Solicitor General Theodore Olson, who delivered the closing argument at trial for opponents of the ban.
California Gov. Arnold Schwarzenegger also praised the ruling as an important step toward equality and freedom.
Protect Marriage, the coalition of religious and conservative groups that sponsored the ban, said it would immediately appeal the ruling to the 9th U.S. Circuit Court of Appeals.
"In America, we should uphold and respect the right of people to make policy changes through the democratic process, especially changes that do nothing more than uphold the definition of marriage that has existed since the founding of this country and beyond," said Jim Campbell, a lawyer on the defense team.
Despite the favorable ruling for same-sex couples, gay marriage will not be allowed to resume immediately.
Judge Walker said he wants to decide whether his order should be suspended while the proponents of the ban pursue their appeal. He ordered both sides to submit written arguments by Friday on the issue.
The appeal would go first to the 9th Circuit then to the U.S. Supreme Court if the high court justices agree to review it.
California voters passed the ban as Proposition 8 in November 2008, five months after the state Supreme Court legalized gay marriage.
Supporters argued the ban was necessary to safeguard the traditional understanding of marriage and to encourage responsible childbearing.
Walker, however, found it violated the Constitution's due process and equal protection clauses while failing "to advance any rational basis in singling out gay men and lesbians for denial of a marriage license."
"Indeed, the evidence shows Proposition 8 does nothing more than enshrine in the California Constitution the notion that opposite-sex couples are superior to same-sex couples," the judge wrote in his 136-page ruling.
He also said proponents offered little evidence that they were motivated by anything other than animus toward gays - beginning with their campaign to pass the ban, which included claims of wanting to protect children from learning about same-sex marriage in school.
"Proposition 8 played on the a fear that exposure to homosexuality would turn children into homosexuals and that parents should dread having children who are not heterosexual," Walker wrote.
Walker heard 13 days of testimony and arguments since January during the first trial in federal court to examine if states can prohibit gays from getting married.
The plaintiffs presented 18 witnesses. Academic experts testified about topics ranging from the fitness of gay parents and religious views on homosexuality to the historical meaning of marriage and the political influence of the gay rights movement.
Olson teamed up with David Boies to argue the case, bringing together the two litigators best known for representing George W. Bush and Al Gore in the disputed 2000 election.
Defense lawyers called just two witnesses, claiming they did not need to present expert testimony because U.S. Supreme Court precedent was on their side. The attorneys also said gay marriage was an experiment with unknown social consequences that should be left to voters to accept or reject.
Former U.S. Justice Department lawyer Charles Cooper, who represented the religious and conservative groups that sponsored the ban, said cultures around the world, previous courts and Congress all accepted the "common sense belief that children do best when they are raised by their own mother and father."
In an unusual move, the original defendants, California Attorney General Jerry Brown and Schwarzenegger, refused to support Proposition 8 in court.
That left the work of defending the law to Protect Marriage, the group that successfully sponsored the ballot measure that passed with 52 percent of the vote after the most expensive political campaign on a social issue in U.S. history.
Currently, same-sex couples can only legally wed in Massachusetts, Iowa, Connecticut, Vermont, New Hampshire and Washington, D.C.
The ruling puts Walker at the forefront of the gay marriage debate and marks the latest in a long line of high-profile legal decisions for the longtime federal judge.
He was appointed by Ronald Reagan, but his nomination was held up for two years in part because of opposition from gay rights activists. As a lawyer, he helped the U.S. Olympic Committee sue a gay ex-Olympian who had created an athletic competition called the Gay Olympics.
Walker is a Republican. He said he joined the party while at Stanford University during the Vietnam War protests, and spent two years clerking for a judge appointed by Richard Nixon.
AP Writer Jennifer Peltz in New York City contributed to this report.