Has the trope of a witness implicating themselves of a crime instead of the defendant ever happened?

So its a pretty common trope in legal shows. Person A is on trial for a serious crime (typically murder), cunning defense lawyer believes Person A innocent and works out it was actually Person B that did the crime. Using their super crafty legal skills they put Person B on the witness stand and surprise convince him to implicate himself. The judge orders the bailiff to clap Person B in handcuffs and releases person A, Cheering all round, brave lawyer walks out to closing credits.

Has anything remotely like that ever happened? I get its a pretty common defense tactic to try and convince the jury someone else did the crime. And more than one person can be prosecuted for one crime, not necessarily at the same time. But has witness testimony (from someone who is not a co-defendant or otherwise implicated in the crime prior to the testimony) ever actually been so implicating (in the crime being tried) it shifted the target of prosecution from the defendant to the witness.

And even if it did could anything like the judge saying “bailiff apprehend that witness, release the defendant!”, actually happen in a real court room in any jurisdiction ?

The best I’ve found is this (which is not what you’re looking for)

". . . I thought he was going to shoot me and I fired the gun. And I didn’t know whether I killed him. I wasn’t trying to kill him . . . . "

With that testimony, Harlow Brian Sails, 21, stunned the jury in his murder trial in Upper Marlboro yesterday as he admitted shooting an off-duty D.C. police officer who had intervened in the robbery of a jewelry store.

What fictional examples come to mind I wonder.

“Legally Blonde” for one

Like, every freaking episode of Perry Mason?

And in fact it’s actually called a “Perry Mason moment”.

I don’t remember the title, but sometime in the 1980’s I read an anthology of short stories from Ellery Queen Mystery Magazine, in which one story, consisting of nothing but dialog until the last sentence, has a district attorney in a murder trial doing direct examination of a police detective on the stand. During it, the DA accidentally reveals knowledge that only the killer could have known, since the police didn’t reveal it to anyone. The detective points that out. The final sentence was something like, “But the district attorney broke down in tears.”

TvTopes calls this the Perry Mason Method.

https://tvtropes.org/pmwiki/pmwiki.php/Main/ThePerryMasonMethod

The case of Harlow Sails noted above is the only entry in the “Real Life” folder.

While the scenario posited by the OP isn’t something I’ve seen, it is possible for the prosecutor to dismiss the case, or offer a plea, in the midst of trial, so it wouldn’t be impossible for them to announce “we’re dismissing the case” because of something that a witness said, as rare as that may be.

(I was recently in a trial where the victim, who accused the defendant of choking her, had decided that she didn’t want him prosecuted by the time the trial started. Since the state was not agreeing to dismiss, she went to trial and testified “I don’t remember” and “I’m not really sure” to practically every question. During a break outside the presence of the jury - where the lawyers were debating the admissibility of her past statements - the judge looked at the prosecution and said, “What do you say? Reduce it to disorderly conduct, give him time served, and call it a day?” But the prosecutor said no, since she “was basically through presenting her case”. She ended up losing the verdict)

Ok that’s weird I literally read this in Upper Marlboro, MD (at a pumpkin patch not a jewellers, but still)

Not an exact fit to the OP, but during the trial of Navy Seal Eddie Gallagher (charged with murdering a POW), the prosecution’s witness (a medic on Gallagher’s team) admitted to killing the POW instead.

Never mind.

This is pretty darn close to what the OP wanted. Thanks.

Yup that’s as close as we are going to get. Though the fact trial continued and resulted in a conviction is not usually part of the trope.

My thought was “So what?” The guy ended up with time served anyway. Best case - he just avoids a conviction record for… ooh! Disorderly conduct! Worst case, he does get convicted. Prosecutor can’t really lose.