LGBT and human rights activists erupted in celebration in Quito and the port city of Guayaquil, waving the rainbow flags that symbolize the gay rights movement.
The Constitutional Court said same-sex marriage had been approved in a five-to-four vote of its nine judges in a closed door hearing.
Ecuador, where the church is very influential, thus joins Argentina, Brazil and Colombia in recognizing same-sex marriage.
"It means that Ecuador is more egalitarian," said lawyer Christian Paula of the Patka Foundation, which provides legal advice for around 10 same-sex couples seeking to marry in the country.
"It recognizes that human rights must be for all people without discrimination."
The four dissenting judges argued that in order to recognize same-sex marriage, constitutional reform would have to be debated in the National Assembly.
Gustavo Medina, a former Supreme Court president, told AFP that Ecuadoran authorities were obliged to abide by decisions of the Constitutional Court, which were "binding and mandatory."
Ecuador has recognized de facto civil unions for same-sex couples since 2015.
The Constitutional Court approved same-sex marriage as it ruled on lawsuits by two pairs of men who wanted to wed.
The men in one of those couples are named Efrain Soria and Javier Benalcazar.
"I want to say hello to Javier, who is in Guayaquil. Honey, I love you," Soria told reporters in the capital Quito.
He urged other gays to stop hiding and "enjoy the happiness that comes from being equal, like anyone else."
Ecuador's current constitution defines marriage as the union between a man and a woman.
The charter, ratified in 2008, also bars same-sex couples from adopting children.
But the judges that approved same-sex marriage said they based their decision on the idea that all people are equal. They also said they sought to counter any kind of discrimination.
In Foch Plaza in downtown Quito, two women who have been fighting since 2013 for the right to marry staged a symbolic wedding right after the court decision was announced.
Pamela Troya and Gabriela Correa sat formally in chairs for the ceremony and when Troya was asked the big question, she said: "Today, I say yes. A big yes. A giant yes. I want to be your wife."
Others in the crowd shouted in approval, hugged and kissed.
Troya later told reporters the fight for same sex marriage in Ecuador had been long and hard, and at times she thought it was a lost cause.
"Finally, today is an historic day for Ecuador because it is a little more fair and egalitarian," she said.