Michael Gambon plays Dumbledore in the Harry Potter films
She made her revelation to a packed house in New York's Carnegie Hall on Friday, as part of her US book tour.
She took audience questions and was asked if Dumbledore found "true love".
"Dumbledore is gay," she said, adding he was smitten with rival Gellert Grindelwald, who he beat in a battle between good and bad wizards long ago.
The audience gasped, then applauded. "I would have told you earlier if I knew it would make you so happy," she said.
"Falling in love can blind us to an extent," she added, saying Dumbledore was "horribly, terribly let down" and his love for Grindelwald was his "great tragedy".
"Oh, my god," Rowling, 42, concluded with a laugh, "the fan fiction".
Fan sites have long speculated on Dumbledore's sexuality as he was known for having a mysterious, troubled past.
Rowling told the audience that while working on the planned sixth Potter film, Harry Potter and the Half-Blood Prince, she saw the script carried a reference to a girl who was once of interest to Dumbledore.
She said she ensured director David Yates was made aware of the truth about her character.
Gay rights campaigner Peter Tatchell welcomed the news about Dumbledore and said: "It's good that children's literature includes the reality of gay people, since we exist in every society.
"But I am disappointed that she did not make Dumbledore's sexuality explicit in the Harry Potter book. Making it obvious would have sent a much more powerful message of understanding and acceptance."
And a spokesman for gay rights group Stonewall added: "It's great that JK has said this. It shows that there's no limit to what gay and lesbian people can do, even being a wizard headmaster."
Rowling also did a brief reading from the seventh book in her best-selling series, Harry Potter and the Deathly Hallows, as part of her Open Book Tour of the US - her first there for seven years.
She said she regarded her novels as a "prolonged argument for tolerance" and urged her fans to "question authority".
But she added that not everyone likes her work. Christian groups have alleged the books promote witchcraft. The author said her revelation about Dumbledore would give them one more reason.
The seventh Potter book broke sales records on both sides of the Atlantic when it was published in July, selling 11 million copies in 24 hours.
The fifth film adaptation of the series, Harry Potter and the Order of the Phoenix was released this summer. Harry Potter and the Half-Blood Prince is due for release late next year.