Donnie Andrews saw his first dead body, lynched and hanging from a tree in North Carolina, when he was four years old. At 10, he watched from behind the washing machines in a Baltimore laundromat as an old man was bludgeoned to death for 15 cents. Physically abused by his mother, enticed by a life of crime, he earned his first long stretch in jail when he was 19. As an armed robber, he swapped holding up bars for a more lucrative and dangerous occupation: robbing drug dealers. In 1986, aged 32, he committed his first and only murder, a shooting carried out at the behest of a local drug lord.

I had always wondered, as I watched The Wire, where an iconoclastic anti-hero like Omar Little – the acclaimed television drama's ruthless, fearless, mercenary yet moral, Baltimore stick-up artist – could possibly have come from. Donnie Andrews is my answer. "When I first met David [Simon, creator of The Wire]," says Andrews, now 55 and a reformed man, "I told him a lot about my little escapades. Then I started seeing them on TV."

Thanks not to America's flawed prison system (where he spent almost 18 years for the murder), but to his conscience, force of will, and the support of friends like Simon, Andrews transformed himself. Today, he is head of security at Bethel AME, one of Baltimore's most prominent African-American churches; and he counsels young gang members, hoping to staunch the flow of murders in Maryland's largest and most violent city.

Download the new Independent Premium app Sharing the full story, not just the headlines

Softly spoken, nattily dressed and enjoying breakfast at a club in London's West End, Andrews is able to look at his past life with the clarity of distance. "That person was buried 15 years ago," he says. "I did it all and I lived through it, so now I think: why push my luck?"

Born in Carolina, Andrews moved to Maryland with his mother and five siblings in the midst of the struggle for civil rights. In Baltimore, he was given away to a carer called Miss Ruth. It was, he remembers, the best part of his childhood. But after Miss Ruth's husband suffered a heart attack, she was forced to return him to his mother.

"When Miss Ruth came back for me later, my mom said she wanted to keep me. I tried to be bad so she would give me back to Miss Ruth, but it just increased the abuse. She used to beat us with extension cords. By the time I was 13, I was on the streets with the gangs, hustling and staying alive."

West Baltimore's housing projects in the Sixties and early Seventies were dangerous for a teenager. Mentored by "hustlers" and drug dealers, as a young gang member, Andrews recalls how "the word 'future' wasn't even in my vocabulary, because I didn't know if I'd be alive or dead tomorrow. They had a bet in my neighbourhood that I wouldn't reach 21. Well, I'm 55 now. And the people who made the bet? They're dead."

Between his 16th birthday and his murder conviction 16 years later, Andrews was arrested 19 times. He spent six years in jail for armed robbery, another two and a half years for daytime housebreaking. His fights with prison guards meant he spent most of that time in solitary confinement. On the outside, like Omar, he preferred to work alone.

"When I was coming up, one of the biggest drug dealers in the city would always tell me a real man stands alone. I felt better working by myself. I only had a couple of friends who I was comfortable hustling with. They'd have to know anything I was going to do just by a look; when you're robbing people, it's gotta be perfect."

Also like Omar, Andrews's victims were fellow drug dealers. "I might get two or three hundred dollars robbing a bar, but from a drug dealer I could get two or three hundred thousand. I told Fran [his wife] about a time I went to rob a stash house and they wouldn't open the door. I yelled: 'If I've gotta come in there, something bad's gonna happen.' The window opened and they threw the drugs out. Fran saw the same thing on The Wire and she called David and said: 'So Omar is Donnie?!'"

Daily coronavirus briefing No hype, just the advice and analysis you need Enter your email address Continue Continue Please enter an email address Email address is invalid Fill out this field Email address is invalid Email already exists. Log in to update your newsletter preferences Register with your social account or click here to log in I would like to receive morning headlines Monday - Friday plus breaking news alerts by email Update newsletter preferences

He had a moral code, of sorts. "I would never mess with women... [and] I wouldn't give kids drugs. That's how the game got messed up: you've got mothers, grandmothers, children of five or six trying to sell you drugs now." Beneath Andrews's violent veneer, there was a conscience lurking. But it was only pricked when he finally killed a man.

After emerging from his latest spell in jail in 1986, Andrews found his neighbourhood under the control of a 25-year-old drug lord named Warren Boardley, whose operation was worth around $250,000 (£150,000) a week. During a shootout over territory that summer, Boardley had been shot in the foot by members of a rival crew, the Downer brothers. A friend of Andrews was shot in the same battle, and he found himself unexpectedly allied with Boardley, who was willing to pay handsomely for a hit.

On the night of 23 September 1986, Andrews and Reggie Gross, one of Boardley's henchmen, cruised the blocks surrounding Gold Street, a neglected terrace that was home to one of West Baltimore's notorious 24-hour drugs markets. When they came across one of the Downer gang – an acquaintance of Andrews known as Fruit Loop – Andrews managed to warn him off, saving his life without Gross's knowledge.

Their next target was not so fortunate. The gunmen found Zach Roach, another member of the Downer gang, sitting with a second young man, Rodney Young, outside a Gold Street house. Gross, carrying a machine gun, opened fire first – killing Young instantly.

"Once Reggie's Uzi went off, [Zach] jumped up and it was a spontaneous reaction on my part. I just fired and, as he ran up the street, he tripped and fell. I went to give him the coup de grâce and he looked up at me. I looked him in the eye and, before he died, he asked me: 'Why?' It was like I was frozen in time. I thought: why? This guy looks just like me. He could have been my brother, my son, my father. And why for drugs? Because somebody shot Warren in the foot? Why? It stuck with me, and I couldn't get it out of my head. I'm trying to figure out why to this day."

His payment, $5,000 and two ounces of heroin, did little to ease his guilt. The Baltimore Police Department (BPD) suspected him of the murder, but lacked evidence. One homicide detective who came knocking was Ed Burns. In early 1987, Andrews ran into Burns at the city court house. "Ed followed me to the parking lot and said: 'I can give you a second chance at life.' I was like, who does he think he is, God? But I thought about it. Even a fool wants a second chance."

Burns's partner made a strange suggestion: Andrews, he said, should read the Bible – specifically the story of Paul. The tale of a brutal tax collector's conversion moved him, as it was meant to. In August 1987, he confessed to the murder, then wore a concealed recording device to meetings with Boardley and Gross, where both implicated themselves in the crime. A prosecutor promised Andrews he'd be free in 10 years. "Donnie was remarkable," says David Simon. "He gave himself up when they had very little evidence against him. Ultimately, it was an act of conscience – and that doesn't happen a lot in police careers."

At the time a police reporter for The Baltimore Sun, Simon spent 1988 shadowing the city's homicide department. There he befriended Burns for his book Homicide: A Year on the Killing Streets, a vivid and meticulous portrait of the period's crime epidemic from the point of view of those trying to fight it.

In 1989, on Burns's advice, he flew cross-country to the Federal Correctional Institution in Phoenix, Arizona, to interview Andrews for an article in the Sun's weekly magazine. "Donnie told me the story of the Boardley case as he knew it," Simon explains. "I was impressed that, when I matched it against the police files, it always checked out. After the article was published, Donnie just kept calling me. I realised that he was really being rigorous about making the most of his second chance."

Andrews had kicked his heroin habit in jail, trained as an electrician, taken up a college course by mail, and even begun to mentor some of the younger inmates. Burns, who retired from the BPD and briefly became a schoolteacher, would send him books. Simon, meanwhile, sent him copies of the Sun: "He'd see some small story about a shooting, then call me a few weeks later with very good information."

Andrews's rehabilitation is, Simon insists, utterly out of the ordinary. "The prison system in America isn't structured for rehabilitation," he says. "It's structured for warehousing ... I believe in the individual's capacity to change their own future. Systemically, though, we sure make it hard. It's a pretty lonesome journey."

In 1992, Simon and Burns had begun work on a new book together, chronicling the lives of a disadvantaged family caught in the crossfire of the drugs war. The Corner: A Year in the Life of an Inner-City Neighbourhood would be published in 1997 and turned into an HBO mini-series three years later.

One of the book's protagonists was Fran Boyd, a heroin-addicted woman with two sons from West Baltimore. The writers, says Simon, had come to love Boyd, and wanted to help her escape the cycle of addiction. "Ed had the idea of putting Donnie and Fran together via a phone call. He had no idea he was playing Cupid."

What followed began as counselling and became a four-year courtship. With each other's help – through phone conversations and letters – Andrews began to come to terms with his crime, while Boyd shook off her addiction. The pair didn't meet in person until 1997, but by then they were already in love, and turned their efforts to winning Andrews his freedom. The city prosecutor who'd promised him an early release reneged on that pledge, and it took a further eight years, until April 2005, before he was paroled.

His first job on release was in the writers' office of The Wire. Eventually, like many Baltimore locals, he found himself cast in the show – as one of Omar's crew. His character was killed in a shootout, from which Omar escaped by leaping from a fifth-floor balcony. "That really happened to me," Andrews chuckles, "but I had to jump out of the sixth floor. It was either lead poisoning or take my chances, so I took my chances. I did it without thinking. If I'd thought about it, I might have taken the lead poisoning."

Andrews and Boyd were married in 2007, and the congregation contained many of The Wire's actors. Simon was best man.

Andrews is still shocked by the decline of his old neighbourhood in West Baltimore. "When I came back," he says, "I actually had tears in my eyes. All the houses that once had families in them are boarded up. The drug addicts are like zombies. I try to do everything I can to rebuild; that's why I took the job at Bethel AME, and that's why I work with the gangs."

How does he persuade young gang members to respect him, to trust him, even to take his advice and step away from a life of violence? "It's like when I met David or Ed. 'Real' recognises 'real'. If you're real and you care about something, it shows. Your actions speak for themselves. When I first met Ed, I could tell he was the type of person that cared; he knew how the street was by working it for 20 years. And he proved it, by sticking with me the whole time I was in prison."

Some of the old habits of the street come in useful as part of Andrews's work. Others simply die hard. "I had a lot of friends who wore their guns in their belts and died because it's difficult to pull from there," he says. "I still always wear baggy shirts out of habit, because I used to keep my gun up my sleeve."

The third series of The Wire begins on BBC2 tomorrow night