The former N-Dubz star was facing a charge of being concerned with arranging a cocaine deal worth £820 last year, after allegedly boasting that she could "sort out" cocaine for Mr Mahmood and put him in touch with her rapper friend Mike GLC who supplied the Class A drug.
She vehemently denied brokering the deal, which was exposed in the Sun on Sunday newspaper last June.
On Monday judge Alistair McCreath told the jury that the case "cannot go any further" because there were "strong grounds to believe" that Mr Mahmood had "lied" at a hearing before the trial started.
It can now be reported that before the trial began, defence counsel argued that the case should be "stayed" - meaning thrown out - but their application was turned down.
But giving his ruling on Monday, the judge said "matters have moved on since then".
The court heard the collapse of the case hinged on evidence given by Mr Mahmood about a statement given to police by a driver called Alan Smith who picked up Ms Contostavlos from a hotel in London in May last year.
Mr Smith originally suggested he had heard the star talking disapprovingly about drugs but "changed his mind" after a conversation with the undercover reporter, the court was told.
They targeted me at a time when things were going badly for me and they had no mercy.
During a pre-trial hearing Mr Mahmood was asked: "Did you subsequently ask or find out, discuss with Mr Smith anything that was said in the car?"
He replied: "No."
Asked if at any stage he discussed Ms Contostavlos saying she "disapproved" of drugs, he also said no.
But the judge said he gave answers which were "entirely inconsistent" when he gave evidence about the same topic at the trial last week.
Standing on the steps of London's Southwark Crown Court, Ms Contostavlos condemned Mr Mahmood as she read out a statement which said: "This whole case was a horrific and disgusting entrapment by Mazher Mahmood and the Sun on Sunday newspaper.
"Mahmood has now been exposed by my lawyers openly lying to the judge and jury. These lies were told to stop crucial evidence going before the jury.
"This evidence showed that I told Mahmood's driver that I disapproved of drugs, which is the truth.
"It is clear that the driver was pressurised to change his statement to strengthen Mahmood's evidence and cause damage to mine. Thankfully the lies have been uncovered and justice has been done."
She added: "I urge the police ... to investigate Mazher Mahmood and his team and to put an end to his deceit in pursuit of sensational stories for commercial gain."
Mike GLC - whose real name is Michael Coombs - pleaded guilty before the start of the trial to supplying 13.9g of cocaine but he also walked free after judge said the case cannot proceed against him.
The Sun has confirmed that Mr Mahmood has been suspended.
A spokesman said: "We are very disappointed with this outcome, but do believe the original investigation was conducted within the bounds of the law and the industry's code. This was demonstrated by the CPS decision to prosecute.
"The Sun, of course, takes the judge's remarks very seriously. Mr Mahmood has been suspended pending an immediate internal investigation."