Incitement to hatred on the grounds of sexual orientation will become a criminal offence after the Criminal Justice and Immigration Bill passed its final stages in the House of Lords last night.

It is expected to receive Royal Assent today.

The government accepted an amendment that will allow people to use a “free speech” defence in cases of incitement.

The amendment was criticised by the minister responsible for the legislation, Maria Eagle.

The bill also moved to ban on prison staff taking strike action.

The agreement between the Prison Officers Association and the government about industrial action expires today, so the whole bill had to be passed last night to avoid possible strikes.

Peers in the House of Lords voted to retain an amendment that will mean some people of extreme views may attempt to avoid prosecution by citing a religious defence and MPs were forced to let it remain in the bill due to time constraints.

Many Conservatives backed this amendment, among them former Cabinet minister Lord Tebbit, a longtime foe of equality for gay people.

Stonewall’s chief executive Ben Summerskill welcomed the new law in a letter to supporters, calling it “another historic step towards legal equality.”

However, he said that the Lords debates were “distressing, and completely gratuitous, references to sodomy and young people during the debate.

“We will push the government before the offence is finally introduced to minimise the impact of this exclusion.

“However, the tone and unpleasantness of the debates in recent weeks have been a very stark reminder of how many veteran opponents of equality there still are in both Houses of Parliament.”

Stonewall, the largest lesbian, gay and bisexual organisation in the UK, has led the campaign for a new offence of incitement to hatred on the grounds of sexual orientation.

The new law was supported by the Board of Deputies of British Jews, who said that their community’s experience of prejudice was the reason they were supporting it.

“We’ve been supported by MPs and peers and also by Sheri Dobrowski, whose son Jody was murdered just for being gay in 2005,” said Mr Summerskill.

“We’re enormously grateful to all of them. But we wouldn’t have been able to secure this further step toward full equality without the constant support, both personal and financial, of so many thousands of our supporters too.”