MSPs have voted to repeal Scotland's Offensive Behaviour at Football Act.
The legislation was passed by the then-majority SNP government in 2011 in a bid to crack down on sectarianism.
But all four opposition parties argued for it to be scrapped, saying it unfairly targets football fans and has failed to tackle the problem.
Ministers argued the move was "foolhardy" but were outvoted by 62 to 60, meaning the Football Act will be taken off the statute book in April.
The legislation has deeply divided opinion from the start, with those who support it saying it was needed to fight the scourge of sectarianism within Scottish football.
But opponents say the law treats football fans as "second class citizens", and is not needed as police and the courts already had sufficient powers to deal with offensive behaviour.
They also claim that the law is badly worded, and therefore open to different interpretations of what is and is not "offensive behaviour".
The drive to repeal the bill was spearheaded in parliament by Labour MSP James Kelly, who said he was "delighted" to see the end of "the worst piece of legislation in Scottish parliament history".
He said that while "every reasonable member" condemned bigotry, the Football Act was poorly drafted and had "completely failed" to make a difference.
Mr Kelly said the legislation treats football fans as "second class citizens", adding that it was "illiberal" and a "legislative disaster", and it was "time for it to go".
He said "a much wider and more serious conversation" was needed to make a difference in tackling sectarianism, saying he would be happy to work alongside the government on that.
Community safety minister Annabelle Ewing said the repeal vote was "deeply disappointing and worrying", arguing that it would compromise the ability of police and prosecutors to charge people for unacceptable behaviour.
She told MSPs that "toxic behaviour" was a "continuing problem" particularly around football, listing incidents around the recent Edinburgh and Old Firm derbies as examples.
Ms Ewing said the bill should be "strengthened and improved" rather than repealed, and said that scrapping the legislation would send the message that parliament was "happy to let this behaviour go unchecked and unchallenged".
The minister put forward amendments to delay the repeal of the bill, and specifically to protect provisions about offensive communications while alternatives are drawn up.
However, these were rejected by MSPs, with opposition members joining together to defeat the government in a series of votes.
In the final debate, a number of SNP members spoke up in defence of the legislation - but opposition MSPs were united in backing repeal.
Scottish Conservative justice spokesman Liam Kerr said the bill was "unnecessary", and treats football fans as a "malevolent" entity.
Green MSP Patrick Harvie said members had a "shared revulsion" for sectarianism, but said this was an argument for good law, not a defence for bad law. His colleague John Finnie - a former policeman who voted for the legislation as an SNP MSP in 2011 - said officers were "perfectly capable" of policing without the Football Act.
And Lib Dem Liam McArthur said it was "wrong and increasingly irresponsible" for the government to claim repeal would leave a gap in the law, saying all offences in the "discredited" Football Act were "fully covered" by existing law.
The Football Act will be taken off the statute book once the repeal bill gets royal assent, which is expected to happen in April - before the end of the current football season.
The Fans Against Criminalisation group which campaigned for repeal called the move "a historic victory for football fans".
What is the Offensive Behaviour at Football Act?
The legislation was drawn up by the Scottish government following a summit in March 2011 to discuss the impact of sectarianism on Scottish football and on wider society.
The summit was called after Celtic manager Neil Lennon and his Rangers counterpart, Ally McCoist, were involved in a heated touchline confrontation during a match.
In the previous weeks, "viable parcel bombs" had been sent to Lennon and prominent Celtic supporters Paul McBride QC and MSP Trish Godman, while bullets were also posted to Lennon and two Celtic players.
The new law, which came into force in 2012, created two new offences - "offensive behaviour at regulated football matches" and "threatening communications".
As well as people actually attending matches, the first of these covers people travelling to and from matches, and those watching the match on TV in a pub.
It involves behaviour that is "likely to incite public disorder", and which is "expressing hatred of, or stirring up hatred against" a person or group based on religion, race, nationality or sexual orientation.
The "threatening communications" section of the law generally covers online and social media abuse.
The law was the first piece of legislation passed at Holyrood with no cross-party support, with every SNP MSP voting in favour - and every opposition MSP voting against.
Speaking on Wednesday, Hibernian manager Neil Lennon - who was targeted by a letter bomb when he was Celtic manager - said he believed there were now fewer incidents of sectarian chants and songs in Scottish football than in years gone by.
But a number of people employed by the league to tackle sectarianism told BBC Scotland that sectarianism was being swept under the carpet, with not enough being done tackle it.
How often has the law been used?
In 2016-17, there were 377 charges reported by police to prosecutors under the offensive behaviour at football section of the legislation.
This was a 32% increase on the 286 charges reported in 2015-16 and the highest number since the bill came into force.
The increase was largely explained by the 140 charges resulting from disorder at the Scottish Cup Final between Hibs and Rangers at Hampden.
There were only six charges under the threatening communications part of the legislation in 2016-17, with seven the previous year and four in 2014-15.