Cook County State’s Attorney Kim Foxx’s decision to step aside from the Jussie Smollett case and hand off her duties to her top deputy landed her a stinging rebuke from a judge Friday — and a special prosecutor investigation. And a former top aide said the recusal decision was Foxx’s alone, contradicting Foxx’s most recent public statement on the matter.

Judge Michael Toomin on Friday ruled that a special prosecutor should further investigate the Smollett case — raising the possibility that Smollett could be recharged. The special prosecutor will also look at how Foxx and her office handled the decision to drop charges against the “Empire” actor.

“The unprecedented irregularities identified in this case warrants the appointment of independent counsel to restore the public’s confidence in the criminal justice system,” Toomin wrote in a 21-page ruling issued Friday.

Toomin didn’t find that Foxx had a conflict of interest in the case but ruled that Foxx should have requested a special prosecutor once she decided to recuse herself in February, shortly before the actor was charged with making a false report to police.

By putting her top deputy Joe Magats in charge, without any legal authority to do so, Foxx effectively invalidated every part of Smollett’s prosecution — from bringing charges before the grand jury to the decision to drop all 16 counts against the actor, the judge determined.

“There was no duly elected state’s attorney when Jussie Smollett was arrested. Ms. Foxx had already effected her recusal,” Toomin said, nor was there any legally appointed prosecutor when Smollett was charged, or the charges dismissed.

Toomin expressed concerned about Foxx’s decision to delegate the case to Magats.

“There isn’t an office of ‘acting state’s attorney.’ It existed only ... in the imagination of Ms. Foxx,” Toomin said.

Foxx and her staff later backpedaled to say that Foxx had recused herself in a “colloquial” sense by delegating decision-making the case to her top deputy.

Toomin wrote in his opinion that “discerning members of the public have come to realize that the ‘recusal that really wasn’t’ was purely an exercise in sophistry.”

In a statement, Foxx said she disagreed with Toomin’s decision to launch a special prosecutor probe and that she had followed the advice of her former chief ethics officer, April Perry, about the recusal decision.

“I am pleased that the court agreed there was no conflict of interest here. Regarding recusal, I followed the advice and counsel of my then Chief Ethics Officer. In any event, I respectfully disagree with the court’s conclusion that, in the absence of any conflict, the appointment of a special prosecutor is required,” Foxx wrote.

Hours later, Perry, a former federal prosecutor who resigned from the office in May, issued her own statement, contradicting Foxx. Perry recounted that the day before Smollett’s arrest, a staff attorney raised questions about whether after her recusal, Foxx could simply delegate decision making in Smollett’s case to her first assistant, Magats. Perry stated she advised Foxx to seek to have Magats appointed as a special assistant state’s attorney. But Foxx declined to follow that advice, Perry said.

“I prepared a motion and order to that effect, and e-mailed it to [Magats] on February 20. Shortly after sending that email, [Magats] advised me that State’s Attorney Foxx determined that the motion and order should not be filed,” Perry wrote in her statement. “As I was unaware of any facts that would demonstrate an actual conflict of interest, I believed that the state’s attorney’s decision on this matter was within her discretion.”

Late Friday, Foxx’s office responded by saying the state’s attorney “out of an abundance of caution followed her ethics officer’s counsel and the precedent Ms. Perry had established for these types of matters. The court ruled today that there was in fact no conflict, and Ms. Perry’s advice was not correct.”

The Chicago Sun-Times was unable to reach Smollett’s attorneys.

Northwestern University Law School professor Locke Bowman, who successfully petitioned for the appointment of a special prosecutor to investigate allegations of torture by former CPD commander Jon Burge, said he was surprised to see Smollett’s case go into the books alongside other historic cases where an independent prosecutor was appointed.

Bowman said he would not be surprised to see Smollett charged again, but based on information that’s been made public, he doubts Foxx or her staff committed any wrongdoing that would rise to the level of a crime, or even a sanction.

”She didn’t need to recuse herself, but Toomin was concerned about the irregularities that followed, so what (Toomin) is saying is, ‘Have a look at this, and have a do-over,’” Bowman said. “But as for any misconduct (by Foxx), making mistakes as an attorney is not deserving of professional sanction.”

The Chicago Police Department pledged to cooperate with the special prosecutor.

“We stand firmly behind the work of the detectives in investigating the fabricated incident reported by Jussie Smollett & #ChicagoPolice will fully cooperate with the court appointed special prosecutor,” police spokesman Anthony Guglielmi tweeted.

State law requires Toomin to first request the Illinois attorney general’s office or the state appellate prosecutor take over the case, then extend the request to the elected prosecutors across the state.

If none of those public agencies volunteer to take on the case, a private lawyer can be hired at county expense. Judge Vincent Gaughan reached out to state’s attorneys in every county in the state in search of someone to handle the prosecution of former Chicago Police Officer Jason Van Dyke. Only Joseph McMahon, the state’s attorney in Kane County, volunteered.

Earlier this year, the Fraternal Order of Police joined suburban police chiefs in demanding the state’s attorney’s resignation. The FOP has also demanded a federal investigation into Foxx’s handling of the Smollett case.

On Friday, Fraternal Order of Police President Kevin Graham called the appointment of a special prosecutor an “excellent move” to make certain that “proper justice is applied” and that “the people of Cook County have been served well.”

“It was improper that [there was] ex parte communications between Jussie Smollett’s attorney and her prior to her making a decision not to prosecute this case or to let it go,” Graham said.

“If a police officer has ex parte communication with a defendant’s attorney, there’d be an investigation because we want to know that things were not handled improperly or that there wasn’t anything improperly going on.”

Retired appellate court Judge Sheila O’Brien in April filed a petition to appoint a special prosecutor, citing her position as a longtime member of the Cook County legal community who felt the local criminal justice system had become a subject of “ridicule” because of the unorthodox handling of the case.

O’Brien included a timeline of events and press accounts, showing that Cook County State’s Attorney Kim Foxx, during the early days of the investigation, had talked about the case with one of Smollett’s family members at the urging of Tina Tchen, a Chicago lawyer and activist who had once served as chief of staff for first lady Michelle Obama. O’Brien also noted that Foxx publicly stated she had recused herself from Smollett’s case, a decision the state’s attorney had made just days before the actor was charged.

Foxx, represented by staff lawyers from the state’s attorney’s civil division, had argued that O’Brien’s petition, if granted, would set a precedent that any citizen could demand a special prosecutor to review any case where they disagreed with the actions of the elected prosecutor or their office. An investigation by the county Office of the Independent Inspector General, which Foxx requested, will provide the same answers O’Brien is seeking, the state’s attorney’s office has argued.

O’Brien also filed a flurry of briefs, motions and subpoenas that ruffled the feathers of the first judge to hear her petition, LeRoy Martin Jr.

O’Brien asked Martin to recuse himself because Martin’s son was a prosecutor in Foxx’s office. Martin grudgingly reassigned the case to Toomin, and O’Brien immediately demanded to know how Martin picked Toomin for the job.

Toomin ruled against nearly all of O’Brien’s requests and canceled subpoenas the retired judge had issued for Foxx, Magats and Tchen. He also ruled against O’Brien’s request that a judge from outside Cook County make the decision.

Toomin, who heads the county’s Juvenile Court division, is the longest-tenured judge in Cook County and has twice previously made the call on whether a special prosecutor was needed to investigate the handling of a high-profile case.

In 2012, he appointed Dan Webb to investigate the death of David Koschman, who died after he was punched by Richard “R.J.” Vanecko, a nephew of former Mayor Richard M. Daley. In 2000, Toomin refused to appoint someone to probe the police investigation of the murder of 11-year-old Ryan Harris, which led to charges against two boys, ages 7 and 8, who were later cleared.

The disputed facts around Smollett’s case still could be aired in a number of other courtrooms. The city under former Mayor Rahm Emanuel filed a civil lawsuit against the actor, demanding that he pay $160,000 spent on police overtime investigating his case. And two brothers who claim Smollett hired them to fake the hate crime attack have sued the actor’s Hollywood lawyers for defamation.