An Alaska man said he survived being pulled off of his bike and attacked by a large brown bear thanks to his quick-thinking friend.

Alex Ippoliti said he and his friend, James Fredrick, were riding along a gravel road on Joint Base Elmendorf-Richardson, a U.S. military facility in Anchorage, Alaska, on Saturday morning when they heard a rustling sound coming from the woods nearby. Ippoliti said the two kept moving, but the bear went for Frederick.

The bear "came out of nowhere and pulled him off his bike," Ippoliti told ABC News.

Ippoliti, a member of the Air Force who was on base that day as part of a recreation program, said he knew he had to think fast in order to save his friend's life.

"When she dropped him, she turned toward me and I was able to spray the bear," he said.

The bear scampered back into the undergrowth and Ippoliti called 911. He then worked with Fredrick to stop the bleeding while they waited for help to arrive.

Black bears kill 2 in Alaska, including teen in extreme race

Black bear attacks hunter in terrifying video

The pair had taken all of the appropriate precautions while traveling along the road, Ippoliti said, and although he has biked in the area many times, he had never encountered a brown bear before.

"We had bear bells on and there was a lot of visibility on the gravel road as we were coming down," Ippoliti said. "We didn't see the bear until she attacked. I had bear spray in a pack on my bike and it was very easy to get it from there, fortunately."

After he sprayed the bear to get her away from Frederick, Ippoliti said they noticed a cub up in a tree. The pair walked down the road as quickly as possible away from the mother and her cub.

"There were a couple times I thought we should stop, but James kept telling me: 'We have to keep going," Ippoliti recalled.

Once they made it about ten minutes down the gravel road, approximately a quarter mile away from the incident, he said they no longer heard the bear.

An environmental conservation officer who patrols the base for animal-related incidents was the first to meet them and Fredrick was taken to Providence Alaska Medical Center in Anchorage where he was treated for his injuries. Fredrick is in stable condition and expected to make a full recovery.

He later posted a photo with Ippoliti next to his hospital bed with the caption, "This dude saved my life yesterday."

Ippoliti said he met Fredrick about a month ago through an amateur race team and their shared interest in riding on gravel trails.

Ippoliti said that after saving his friend, he hit the trails again the next day with his son. During a mountain bike race on Sunday, about 15 miles away from the attack, he said his son and other riders encountered a black bear, but no one was hurt.

"It's a perception lately that there seems to be more [bear attacks] than normal. Everyone is kind of on edge about it right now," Ippoliti said. "I still plan to go ride and do whatever I can to be smart about it and stay prepared so that I can be safe."