LOS ANGELES — With just over 10 minutes left Saturday, guard Zane Beadles gathered his offensive-line mates on the sideline and delivered a high-decibel speech that several players would later reference.

As Beadles spoke, the 49ers trailed the Rams by 14 points and their comically inept and injury-ravaged offense had produced seven punts and two turnovers on its previous nine possessions dating to the first quarter.

“Man, he gave us a great pep talk,” guard Andrew Tiller said. “And we went out there and things finally started clicking.”

No kidding.

In a comeback no one saw coming, the 49ers, a one-win outfit more than three months removed from their last win, overwhelmed Los Angeles down the stretch and won 22-21 on quarterback Colin Kaepernick’s two-point conversion run with 38 seconds left.

The win put an end to their humiliating 13-game losing streak and prompted this question: Why didn’t Beadles start delivering sideline speeches in Week 2?

The 49ers scored 15 points in the final quarter after collecting 20 in their previous nine quarters. In addition, Kaepernick had 150 of his 266 passing yards on the final two drives, and the 49ers (2-13) outgained the Rams 144-4 in the final 10-plus minutes.

This, from a team that was without five season-opening starters because of injuries before it lost running back Carlos Hyde to a knee injury early in the fourth quarter.

“Right before we went on the field, I just kind of said it doesn’t matter what’s happened so far in this game,” Beadles said. “We’ve got 10 minutes left to go win this game right now. Let’s go do it.”

Kaepernick, unprompted, referenced Beadles’ talk when his news conference opened with a how-the-heck-did-the-49ers win question.

“Zane rallied the offensive line on the sideline,” Kaepernick said. “…And I think they responded really well to that.”

After Beadles spoke, Kaepernick delivered.

He directed back-to-back touchdown drives of 75 and 73 yards, capping the first march with a 13-yard scamper. He finished the second drive with a 10-yard strike to wide receiver Rod Streater.

His two-drive totals: 15-of-19 for 150 yards.

And he saved his best for last after head coach Chip Kelly decided to go for the win with a two-point conversion. Kelly said he thought his players were excited about the go-for-broke decision, and defensive tackle Quinton Dial confirmed that.

Dial’s reaction when he saw Kelly was going for two: “Hell, yeah.”

And there was more excitement after Kaepernick made the gamble pay off. He rolled right, couldn’t locate an open receiver, and leaped into the right corner of the end zone with the ball outstretched.

“In my head, it was just, ‘I have to get in,’” Kaepernick said. “‘I have to get in for this team. We need this win.’ I was very excited to be able to do that.”

Kaepernick’s excitement might not have been shared by a segment of the 49ers’ fed-up fan base, which celebrated on social media before the game when previously winless Cleveland beat San Diego. The Browns’ win meant the 49ers would have won the first pick of the 2017 draft if they lost their final two games.

However, in the cramped visitors’ locker room at the Los Angeles Memorial Coliseum, it was clear the 49ers were thrilled their long-awaited win No. 2 hurt their chance for pick No. 1.

“Man, that s— feels real good,” said Hyde. “It takes away this pain that I’m feeling in my knee.”

Said Tiller: “It feels awesome, man. It feels so great. Finally got a win.”

The victory was sealed when rookie Rashard Robinson intercepted quarterback Jared Goff’s pass at the 49ers’ 42-yard line with 16 seconds left.

The pick touched off a wild sideline celebration for a 2-13 team that hasn’t beaten an opponent besides the Rams in more than a year. Consider: The 49ers are 3-0 against the lowly Rams (4-11) and 0-16 against the rest of the league in their past 19 games.

Still, no sense in being a downer. They probably deserved an early holiday party given the season they’ve endured.

“We’ve been working our tails off the whole season, and that ‘W’ was past due,” rookie defensive tackle DeForest Buckner said. “It was just a really good early Christmas present.”

Eric Branch is a San Francisco Chronicle staff writer. Email: ebranch@sfchronicle.com Twitter: @Eric_Branch

3 notables

WR Rod Streater: He had six catches for a game-high 63 yards, and his 10-yard touchdown with 38 seconds left set the stage for the game-winning two-point conversion.

DT DeForest Buckner: The No. 7 pick had his sixth sack of the season, tying for the fifth-most by a rookie in franchise history.

CB Rashard Robinson: The rookie had the game-sealing interception and was part of a secondary that allowed fewer than 100 passing yards for the first time since 2014.