NEW YORK (AP) _ The city where the nation's homosexual rights movement started 17 years ago in riots sparked by a police raid on a gay bar has passed anti-discrimination legislation after the failure of seven previous attempts.

The City Council voted 21-14 Thursday to approve the measure banning discrimination on the basis of sexual orientation in housing, employment or public accomodations. A violation can result in a $500 fine and a year in jail.

''It's OK to be gay and lesbian in New York 3/8'' Joyce Hunter, a leader of the Coalition of Gay and Lesbian Rights, told about 1,000 cheering people who turned out in Greenwich Village later Thursday to celebrate in the neighborhood where the 1969 raid on the Stonewall Inn sparked gay militancy.

New York's first gay rights bill was introduced 15 years ago, the nation's first such measure. It failed in committee five times and was defeated on the council floor a sixth, in 1974. Since 1971, gay rights ordinances have been enacted by Wisconsin, a dozen counties and more than 50 communities.

Mayor Edward I. Koch, who must under the City Charter hold a public hearing before signing the bill into law, said he expected to sign it next week.

''Ultimately, democracy prevailed,'' Koch said. ''That is what is so wonderful about democracy.''

The measure was opposed by Roman Catholic Cardinal John J. O'Connor and leaders of the city's large Orthodox Jewish community.

Councilman Noah Dear, whose district is largely Jewish and who led council opposition, vowed to challenge the bill in court and, if necessary, try to take the issue to voters in a referendum.

''Overwhelmingly, the people of the City of New York will defeat the gay rights bill,'' Dear said.

The Roman Catholic Archdiocese of New York said in a statement it would try to determine what could be done to overturn the bill.

''Yeah, we won, we won, we won,'' Buddy Noro, one of 40 supporters, shouted outside City Council chambers. ''Thank God, we are free in New York City.''

A smaller number of opponents sat quietly.

Hundreds of people gathered in Sheridan Square in the Village to celebrate the vote. People hugged, jumped up and down and applauded exuberant speakers.

''We liberated City Hall today,'' said Alan Roskoff, a gay Democratic activist. ''We liberated New York. This is just the beginning.''

Andy Humm, a leader of the Coalition of Lesbian and Gay Rights, also praised Councilman Victor Robles, who had spoken on the Council floor of his efforts to square his affirmative vote with his religious beliefs.

''Victor said he never called this the gay rights bill, he called it the human rights bill,'' Humm said. ''How does it feel to be a human being?''

The voting produced several moments of drama, one a speech by the Rev. Wendell Foster, a councilman who had been uncommitted.

''I find the gay life repugnant. I find it a deviation and sinful,'' Foster said before his vote.

''In the spirit of Christ, I must love my homosexual brothers and sisters, even though I don't understand them, they frighten me, they intimidate me,'' he said. ''Even though I would like to be 10,000 miles away from here today, I vote yes.''