On Friday afternoon, three weeks after escaping from the maximum-security Clinton Correctional Facility, Richard Matt was shot and killed by federal agents in the upstate town of Malone.

David Sweat, who masterminded the prison break with Matt, escaped the mayhem, but was shot by state police and captured Sunday.

As this odyssey ends, questions remain. Just who were Richard Matt and ­David Sweat? How did these two convicted murderers pull off one of the most complicated prison breaks in modern American history? And how much help did they really have on the inside?

‘Ricky’ Matt

Richard Matt grew up with his brother, Robert, in Tonawanda, a small town north of Buffalo. They were raised by foster parents well regarded in the community, who have since died. The boys’ biological father, also named Robert, was a career criminal.

Even as a child, Richard Matt was menacing. “He would terrorize kids on the bus,” Randy Szukala, a former chief of police for North Tonawanda, told the Rochester Democrat and Chronicle. “Friends of mine knew him. He would just terrorize people. Even in elementary, junior high, he had issues.”

Matt was arrested eight times from 1985 to 1991, on everything from misdemeanor harassment to felony assault.

“One time he beat up a girl pretty bad,” Tonawanda police Capt. Frederic Foels told the paper. “We always knew him as Ricky. ‘Ricky Matt did this, Ricky Matt did that.’ We were very familiar with him at that time in the late ’80s. But the things he was charged with later in life — wow.”

In 1986, Matt set off a four-day manhunt after escaping from an Erie County jail. He was found in Tonawanda at his brother’s house. He was released in 1988 and then served six months in 1990 for violating parole. In 1993, Matt was back in prison for attempted burglary. He did three years, but violated parole once out and wasn’t released again until 1997.

That year, Matt was hired by William Rickerson, a Tonawanda man who had a small business re-selling nearly expired food. Matt lasted just a few months before getting fired, and so one snowy night that December, he and ­accomplice Lee Bates attempted to rob Rickerson, then 76.

They bound Rickerson with duct tape and beat him repeatedly, even though he insisted he had no money stashed. The two men ate pepperoni pizza, drank wine and then, as Bates would later testify, Matt dumped the rest of the wine over Rickerson, who was dressed only in pajamas. Then Matt tore off Rickerson’s toupee, shoved it in his pocket and put him in their trunk.

They drove for nearly 30 hours, crossing state lines.

At one point, Matt opened the trunk and bent Rickerson’s fingers back until they broke. Eventually, Matt killed Rickerson with his bare hands, breaking the man’s neck. Then he dismembered the body with a hacksaw and threw the remains in the Niagara River.

A few weeks later, Rickerson’s remains washed up, and Matt told his half-brother, Wayne Schimpf, that he was in trouble and needed to leave town. “I remember his words,” Schimpf later testified. “ ‘I can do another seven years, but I can’t do life.’”

Matt asked for Schimpf’s car. Schimpf refused and would testify that Matt said: “You’re my brother. You’re my blood. I love you, but I’ll kill you.”

Matt took the car and made his way to Mexico, where, in 1998, he was imprisoned for stabbing an American engineer to death in a bar. He spent nine years in prison there before his unexpected extradition to the United States in 2007. Mexican authorities simply put Matt on a plane.

“The United States had a deal with the Mexican government to extradite a drug-cartel kingpin,” veteran court reporter Rick Pfeiffer told the Democrat and Chronicle. “He was being flown back to Texas and . . . this second guy gets off the plane. It took federal marshals almost a day to figure out who this guy was. There had been no discussion with the American government. He had just been such a difficult prisoner — if you can imagine a guy who seemed too difficult to stay in a Mexican prison.”

Matt returned with metal front teeth and a bullet wound — sustained, he said, while attempting yet another escape.

At his trial in 2008, Matt was wired with electrodes. A sniper was posted on a nearby rooftop. All glass, including sheets over courtroom tables, was removed for fear Matt would break it and use the shards as weapons.

His trial lasted one month, and it took the jury only four hours to find him guilty. He was sentenced to 25 years to life, which he had been serving at Clinton.

“Of all the cases I’ve tried,” said prosecutor Joseph Mordino, who had 250 homicides behind him, “this would top my list for the death penalty.”

David Sweat

As a boy, David Sweat was both methodical and violent. He set his toy cars on fire. When he was just 9 years old, he threw knives and a rocking chair at his mother and brought a butcher’s knife to school. His mother told the Binghamton Press & Sun-Bulletin that his sadism caused her to have at least one breakdown.

She sent Sweat to live with relatives in Florida, but he was no longer welcome after stealing and crashing his aunt’s car. There is no record of him attending high school, and he went into a group home.

At age 16, Sweat was found by a counselor to have written plans to rob the home with a friend. His to-do list included finding blueprints, tying up a woman, putting her in a storage room, and stealing computers and cash.

At his hearing, Sweat blamed fellow “troublemakers” and his own teen idiocy. He was sentenced to five years’ probation, and one year later, in 1997, was convicted of second-degree burglary. He was sent to prison in Broome County, where he kept another to-do list — this one a series of crimes he planned to commit if granted furlough. (He was not, and served 19 months.)

Sweat’s next plan, which he had also mapped out, was to burglarize a gun and fireworks store in Great Bend, Pa. It was early on July 4, 2002, when a then-22-year-old Sweat and two accomplices crashed a stolen truck through the front door and followed his sketch of the store’s interior to find and swipe guns and knives.

At 4 a.m. that day, Sheriff’s Deputy Kevin J. Tarsia, 36, was on patrol when he saw three men huddled around two vehicles in a park.

Tarsia approached, flashlight on. As Sweat would later confess, he shot Tarsia more than once, and as the wounded deputy writhed on the ground, Sweat ran him over and dragged him around the parking lot, breaking the cop’s ribs and his right femur.

He was still alive when Sweat’s accomplice — his cousin Jeffrey Nabinger — rummaged through Tarsia’s patrol car, stealing flares and arrest forms. They took his .40-caliber Glock, and Nabinger later confessed to shooting Tarsia twice in the face.

Tarsia lay dead for two hours before his body was discovered by a man walking home from a night shift. It was riddled with 15 gunshots.

Sweat was caught two days later, and in 2003 he took a plea deal to avoid the death penalty. He got life without parole.

At the time, Tarsia’s fiancée, Christi-Ann Ciccone, said Sweat and Nabinger “really do not ­deserve to live. These are not people. They are monsters.”

The escape

Sweat, 35, had been an inmate at Clinton, in upstate Dannemora, since 2003. Matt, who turned 49 one day before he was shot to death, began serving his term at the prison in 2008.

It’s not known how Matt and Sweat met, but in the months before their escape, good behavior earned them cells on the prison’s “honor block.” There they were granted several privileges, including the right to wear civilian clothes and cook their own meals — even keeping hot plates and ­refrigerators in their cells.

Sweat was allowed to work in the prison’s tailor shop, and sometime over the past year he allegedly began having sex with married prison worker Joyce “Tillie” Mitchell, 51, in an 8-by-15-foot stockroom. Recent inmate Erik Jensen told The Post that Mitchell was like a bashful schoolgirl whenever Sweat was around.

“The way I can describe it is in high school, when one of the good-looking jocks looks at the ugly girl or asks her to prom — that look on her face,” Jensen said. “She was ecstatic.”

Jensen said the other inmates knew exactly what was going on. “Everyone used to joke with him that he was getting his quiet time with his ‘boo,’ his girlfriend,” he recalled. “It was like the running joke in the tailor shop.” He estimates “that Sweat and Mitchell had sex at least 100 times — 30 minutes, four times a week.”

He also said the stockroom had a window with a view — of the very manhole Matt and Sweat would eventually use to escape.

Mitchell brought treats for Sweat, packaging hamburgers, hot dogs, barbecue chicken and Subway sandwiches in Styrofoam containers from the mess hall.

Mitchell allegedly began having sex with Matt as well. Retired Detective David Bentley, who helped put away Matt for Rickerson’s murder, told The Post, “When [Matt is] cleaned up, he’s very handsome and, in all frankness, very well endowed. He gets girlfriends anyplace he goes.”

Mitchell persuaded officials to house Matt and Sweat in neighboring cells. She allegedly began smuggling in power tools in baked goods.

Last Wednesday, another correction officer, 57-year-old Gene Palmer, was arrested for allegedly providing Matt and Sweat with art supplies and power tools. He allegedly allowed Matt to change the electrical box in his cell “to enhance their ability to cook.” That gave Matt access to the catwalk behind it. Palmer said he was paid in art — Matt, a prolific painter, specialized in portraits of celebrities such as Julia Roberts and Marilyn Monroe.

On a portrait of Oprah Winfrey, Matt wrote, “She changed so many lives. Thank you, Oprah.”

In the days after the escape, it’s believed Palmer burned the paintings Matt had given him. Palmer had worked at the prison for 27 years and for much of that time has hated it.

“With the money that they pay you, you’ll go bald, you’ll have high blood pressure, you’ll become an alcoholic,” he told North Country Public Radio in 2000. “You’ll divorce, and then you’ll kill yourself.”

Recent reports reveal that conditions at Clinton are hardly the stuff of maximum security. Before the escape, watchtowers went unmanned at night. There were no hourly, flashlit walk-throughs, because inmates complained that the light interrupted their sleep.

Most shockingly, a Fox News report from outside the prison walls caught a civilian approaching the 30-foot-high wall and watching as a rope was lowered from the other side. The person tied a bag to the rope, and the rope pulled the bag up and over. The whole thing took less than a minute. Two local restaurant managers told NPR that in the past they had delivered takeout to prison guards that way.

In the months leading up to their escape, Sweat and Matt tacked notes to the walls of their cells. They were able to plot and scheme and cut and hack away alone, covering their bars with sheets — a practice allowed by the prison. It’s believed that they made several dry runs, and that they were either given blueprints of the facility or were given enough information to know which pipe would lead them to the right manhole.

On the night of Friday, June 5, Joyce Mitchell left Clinton with her husband, Lyle, who also works at the prison. They stopped at a Chinese restaurant, had dinner, and on the drive home, Joyce told Lyle she was having a panic attack. He rushed to the hospital, unaware that she was allegedly supposed to be meeting Matt and Sweat in a getaway car — and that the men planned to kill him.

Back at the prison, Matt and Sweat waited until lights out. Then they quietly took their clothes, made makeshift dummies and placed them under their blankets.

They shimmied through a hole they had cut in the wall, and then climbed out and up onto the six-story-high catwalk that led to the 400-foot-long, 2-foot-wide pipe that they had already cut a hole through. On the pipe, they left a Post-it note with a racist drawing and the words, “Have a Nice Day!”

It was 5:30 in the morning before guards did a true spot-check and rang the alarm. Whether minutes or hours before, Matt and Sweat had already quietly emerged from that manhole and out into the night.