The 20-year-old man spotted a German shepherd wandering alone in an Eastvale, Calif., neighborhood Sunday night.

It was around 11 p.m., and the man felt sorry for the dog, according to authorities. So he brought it a bowl of water from a house.

The dog took a drink. Then, the animal placed both of its front paws on the man’s chest and attacked, according to Riverside County Animal Services.

The male German shepherd took “a bite out of his left bicep area,” wounded the man’s leg and ankle, and dragged him into the street, according to the animal control agency.

The man screamed for help, the agency said.

Friends and family tried to fight off the dog by punching and kicking it — but it wouldn’t let go, officials said.

“Ultimately, in a desperate attempt to save the man, some of the men grabbed steak knives from inside the home, ran back out, then they stabbed the dog until it finally let go,” Riverside County Animal Services said in a statement.

The man’s uncle told Animal Control Services Officer Will Luna that rescuers had “no choice but to start stabbing the dog in order to get the dog to stop attacking.”

By the time Luna arrived, 911 had been called — and the victim had already been transported by emergency responders to a local hospital, according to the animal control agency.

The injured man’s condition is unknown.

The German shepherd, meanwhile, sat on the front lawn where the attack occurred.

The dog looked lethargic “but immediately sat to attention” when Luna approached, the agency said.

Using his control stick, the officer calmly placed a loop around the dog’s head.

Other officers nearby were worried — but not Luna. He realized this was no ordinary dog; it was a police dog.

“I walked him to my truck and, with one command, the dog, despite its injuries, leaped into one of my truck compartments,” Luna said in a statement. “That dog must have recognized me and uniform and as someone of authority. He didn’t show any aggression toward me at all.”

Luna transported the dog to an emergency animal hospital, but its wounds were too severe and staff had to euthanize it, according to officials.

Later, they discovered a microchip embedded in the dog.

It was a retired K-9 officer, Riverside County Animal Services confirmed.

The dog once worked for the Long Beach Police Department, spokeswoman Nancy Pratt told the Riverside Press-Enterprise.

It joined the force in 2009 as a 2-year-old named Thor, the Los Angeles Times reported. But the dog retired in April because of age and medical issues, Pratt told the newspaper, and a Long Beach Police officer, not its handler, adopted it.

Thor is no longer associated with the department, Pratt told the Times.

Individual officers who wish to become handlers share in the purchase cost of dogs for K-9s, according to the Long Beach K-9 Officers Association.

Just last week, a Long Beach K-9 was shot and killed in the line-of-duty, as police and U.S. marshals tried to capture a suspect in a 2014 shooting. The 4-year-old Belgian Malinois named Credo was struck by friendly fire in an incident that also left the suspect dead, according to police.

“It is important to remember that the dogs are part of the officers family and go home with them every night,” the Long Beach K-9 Officers Association said in a statement regarding Credo’s death. “Events such as today are devastating to the officers and their family as well as the department and community.”