Quarterback Philip Rivers will lead the Chargers into a divisional-round matchup at New England next Sunday. (Toni L. Sandys/The Washington Post)

The Los Angeles Chargers learned their lesson after their regular season loss to the Baltimore Ravens and again resembled what they had been for most of the past few months: one of the AFC’s top teams.

They shut down the Ravens’ offense, built around the running of rookie quarterback ­Lamar Jackson, for most of the afternoon. They did just enough on offense. They got the benefit of a close instant replay ruling at a key moment and then held on, barely, at game’s end. The Chargers earned a trip to New England for a divisional-round game next weekend by beating the Ravens, 23-17, on Sunday at M&T Bank Stadium in an AFC first-round playoff game.

“It was typical of this team this year, the way we won,” Chargers quarterback Philip Rivers said. “We’ve won in so many different ways — ugly, pretty, defensively, offensively, come from behind, make a field goal at the buzzer. It was a collective team win today against a great opponent. My gosh, that [Ravens] defense is rough to deal with.”

The fifth-seeded Chargers stumbled down the stretch, squandering their chance to overtake the Kansas City Chiefs for first place in the AFC West and the top seed in the AFC. A Week 16 defeat to the Ravens in Los Angeles was their undoing, relegating the Chargers to a wild-card spot despite being a 12-win team.

But they were the first team to face Jackson for a second time since he took over for an injured Joe Flacco as the Ravens’ starter in November, and the Chargers made the proper adjustments. They played safeties at linebacker, meaning there often were seven defensive backs on the field. They gave Jackson nowhere to run, and he could not summon the passing accuracy from the pocket needed to make the Chargers pay until it was too late.

“I feel like the first time we played them, we played their game,” Chargers Coach Anthony Lynn said. “Today, I think we played our game. Our defense was outstanding. . . . The more times you see that offense, the better you’re going to play against it.”

Jackson had his best moments in the late stages of a 14-for-29, 194-yard performance. He threw two touchdown passes to wide receiver Michael Crabtree as the Ravens rallied from a 23-3 deficit. The Ravens had a chance to win in the final minute, but their last-gasp drive ended just shy of midfield when Jackson lost a fumble on a sack.

“They came in here looking for a fight, and we were going to give it to them,” Ravens linebacker Matthew Judon said. “Unfortunately the time ran out, and we couldn’t get the job done.”

It was the seventh time Jackson was sacked in the game. The home crowd booed the feeble efforts of the Ravens’ offense earlier in the game, and Jackson had a passer rating of 0.0 well into the second half. Even so, Coach John Harbaugh opted against turning to Flacco.

It was a frustrating ending to a promising season for the fourth-seeded Ravens. They overtook the Pittsburgh Steelers for the AFC North title, returned to the playoffs following a three-season drought and established Jackson, the final player chosen in the first round of last year’s NFL draft, as a potential franchise centerpiece.

Now the Ravens face an offseason in which they probably will part with Flacco, their Super Bowl-winning quarterback. They have said that they will retain Harbaugh for next season and are negotiating a contract extension with him. But other NFL teams with coaching vacancies could come calling.

Harbaugh said he didn’t believe he had coached his final game for the Ravens. He said that Jackson is the starter at quarterback going forward and added that Flacco is “going to have a market” for his services.

The Ravens’ best chance ­Sunday, until their final flurry, came when they trailed 12-3 in the opening moments of the fourth quarter. Chargers tailback Melvin Gordon reached for the end zone with the football after being tripped by Ravens safety Eric Weddle on a goal-line run. The ball slipped from Gordon’s grasp as he landed. Ravens cornerback Marlon Humphrey picked up the loose football and raced more than 100 yards to the opposite end zone. The officials signaled a touchdown for the Chargers, triggering an automatic replay review.

The range of outcomes ranged from a Chargers touchdown to Gordon being down by contact inside the Baltimore 1-yard line to a turnover and a Ravens touchdown. The replay ruling was that Gordon was down by contact inside the 1; the Chargers scored a fourth-down touchdown on the following play and added a two-point conversion for a 20-3 lead.

“I was real confident it wasn’t going to be ruled a fumble,” Lynn said. “I thought it was going to be ruled a touchdown.”

It was the Chargers’ first postseason victory since the 2013 season. They will be a formidable foe Sunday for a Patriots team that has looked vulnerable at times while securing the AFC’s No. 2 seed. The Chargers are 8-1 on the road this season, the lone defeat coming in L.A. to the crosstown Rams back in September.

“I’m looking forward to it,” Lynn said. “A lot of times, you have to go through New England if you want to accomplish your goals.”

The Chargers were in control most of way against the Ravens. They led 12-0 after the first half, in which the Ravens managed just 69 yards. The Ravens fumbled three times in their first eight offensive plays.

“I think we rattled him a little bit just with the pressure and with the D-line,” Chargers rookie safety Derwin James said of Jackson.

Rookie kicker Michael Badgley had four first-half field goals for the Chargers and five for the game. The Ravens got a third-quarter field goal from kicker Justin Tucker after a Chargers fumble. But Tucker missed wide right from 50 yards after the Ravens partially blocked a punt, and the Chargers came up on the right end of the replay ruling on Gordon’s non-fumble to stay comfortably in front until the game got interesting late.