Escaped murderer Richard Matt refused to be taken alive — and died in a hail of gunfire near the Canadian border on Friday in a bloody last stand with officers who had been hunting him for three weeks.

He was caught because he fired a bullet at one point that struck a camper in a possible attempt to make it stop so he could hijack it.

The driver alerted cops who rushed to the scene.

Then, State Police caught a whiff of gunpowder in a nearby cabin, prompting a Customs and Border Patrol tactical team to fly into the area.

“There was indication that someone had recently been there and had fled out the back door,” State Police Superintendent ­Joseph D’Amico said.

The federal agents finally closed in on Matt, 49, in a heavily wooded area near Lake Titus in the tiny town of Malone. The officers heard coughing “so they knew they were dealing with humans, as opposed to wildlife,” D’Amico added.

As they confronted Matt, he gripped a shotgun, according to a law-enforcement source.

“They verbally challenged him, told him to put up his hands, and he was shot when he didn’t comply,” D’Amico said.

They recovered the 20-gauge shotgun, he added.

Fellow fugitive David Sweat — a convicted cop killer — remained at large Friday night and was being tracked by police dogs.

“We have no reason to believe that Mr. Sweat was not with Mr. Matt at the time [he was shot], but we don’t have any confirming evidence that he was there either,” Gov. Cuomo told a press conference Friday night.

“We don’t have anything to confirm where Mr. Sweat is at this time.”

He warned that “these are dangerous, dangerous men.”

Matt’s death came nearly three weeks after the convicts busted out of Clinton Correctional Facility on June 6 in an elaborate escape allegedly facilitated by prison workers.

The manhunt for Matt and Sweat, 35, involved more than 1,100 law-enforcement officials, whose big break came at about 2 p.m. when the driver of the camper called.

“Originally they had heard a sound,” D’Amico said. “They thought maybe they had a flat.

“When they pulled into a campsite, they realized there was a bullet hole through the back of the camper.

“We believe that the gun was stolen by the convicts from a hunting cabin in Mountain View” where they had holed up soon after busting out, the source added.

Bob Willett, who owns an auto-repair shop in Malone, also called police after noticing a bottle of cheap gin had been opened in his son’s cabin in Malone.

“It wasn’t much, but something was out of place,” he said. “The bottle is always on the counter and it was on another counter and the cap was off and it was spilt.”

Matt met his end less than 20 miles away from the border and roughly 50 miles west of the Dannemora prison — on Route 30, near Route 41.

By Friday evening, authorities had formed a perimeter and were following Sweat’s scent with a team of dogs through thick woodland around Lake Titus, but he wasn’t within sight.

They had found two sets of footprints in the woods, including one that they believed belonged to Sweat, the sources said.

Devin Murtagh, 20, said her mom lives only a couple of miles away from where Matt was caught, and remained on edge knowing that Sweat was still on the loose.

“It’s a residential area,” Murtagh said. “[My mother’s] freaking out right now. She’s in the house and really nervous.”

State Police considered both Matt and Sweat armed and “very dangerous” ever since the raid on the Mountain View cabin, where an “inordinate amount of weapons” was stored, State Police Major Chuck Guess said.

A trail camera in Whippleville also snapped a photo of the escaped convicts earlier this week, showing Matt toting a shotgun, law-enforcement officials told The Daily Beast.

The cameras caught them because hunters had been asked by cops to turn them on early, several months before the start of the deer-hunting season on

Oct. 1.

Searchers zeroed in on the area around Malone Thursday night, after linking Matt’s DNA to a second cabin that had been broken into in the area.

Police had also found evidence they had stayed at an outdoor site nearby.

“We discovered through search teams a camp where maybe somebody had laid down and we found candy wrappers and some other things that were left behind that were all searched,” D’Amico said.

The fugitives poached some “basic items” from the sites and left others behind, Guess said at a press conference Friday.

Officials believed the duo had been heading north-northwest in an attempt to make a break for Canada, so authorities formed a “picket line” to ensure they couldn’t make it across, Guess added.

The intensive search began nearly three weeks ago when Matt and Sweat executed an ingenious escape from New York’s largest prison.

During the “Shawshank Redemption”-style breakout, the inmates, who were in adjoining cells, managed to cut their way out of their cells, then walked along a 6-foot-tall catwalk to cut their way into pipes and tunnels that led outside.

They eventually climbed out of a manhole beyond the walls of the prison.

The convicted criminals had help from prison worker Joyce “Tillie” Mitchell, who allegedly had sex with both men behind bars.

She hooked up with cop killer Sweat at least 100 times in a sewing-shop storage closet where they were both assigned, said a former prisoner.

Mitchell smuggled Matt and Sweat tools in frozen hamburger meat, which she allegedly asked guard Gene Palmer to deliver to the inmates in the prison’s “honor block.”

Matt gave Palmer some of his artwork and intel on bad inmates in exchange for his help. Palmer allegedly let the convicts tinker with an electrical box behind their cells, allowing them to access the catwalks they later used to make their escape.

Mitchell and Palmer have both been arrested. Palmer insists he never knew he was sneaking tools to the inmates when he delivered the meat.

Palmer is charged with promoting prison contraband and tampering with evidence for burning the paintings at his house.

Mitchell is charged with criminal facilitation and promoting prison contraband.

Additional reporting by Kevin Fasick and Dana Sauchelli in Malone, NY