ARLINGTON, Texas (AP) — Kyle Shanahan walked onto the field with an emphatic nod, believing his San Francisco 49ers had beaten the Dallas Cowboys in a chaotic wild-card finish that left many not sure what was happening.

The official word came a short time later.

Deebo Samuel ran 26 yards for a touchdown for a comfortable lead in the third quarter before the Cowboys rallied and ran out of time trying to get a final play in the 49ers’ 23-17 victory Sunday.

“I’m still trying to figure out where I’m at,” Shanahan said after coaching San Francisco’s first playoff victory at Dallas in a storied postseason rivalry. “It’s pretty emotional out there. Lots of opportunities to win the game. Those guys (Cowboys) just kept fightin’. We made a couple mistakes there at the end. We shouldn’t have let it get there.”

The 49ers (11-7) overcame an interception by Jimmy Garoppolo when they led by 13 in the fourth quarter, and now are headed to a divisional playoff next Saturday night at Green Bay two years after reaching the Super Bowl and losing to Kansas City.

The wait for Dallas (12-6) to reach even an NFC championship game will tick up to 27 years after another first-game flameout in the postseason for Dak Prescott, the second in three trips over six seasons for the star quarterback. It was his first playoff game since signing a $40-million-a-year contract in the offseason.

Prescott ran for a touchdown to get Dallas within a score after Garoppolo’s interception, and he had plenty of time to get his team in front for the first time on a drive that ended near midfield. His desperation fourth-down pass was just out of receiver Cedrick Wilson’s reach.

A 14th penalty from the NFL’s most-penalized team in the regular season helped San Francisco run out most of the clock, and the 49ers intended to go for the win on fourth down when a run from Samuel ended up short — by inches after the ball was nudged up on review.

Instead, San Francisco punted after a false start, giving Dallas one more chance. Without enough time, as it turned out.

The Cowboys started at their 20 with 32 seconds to go. Wilson pitched to CeeDee Lamb on a hook-and-ladder play for 20 yards, and Dalton Schultz gained 9 yards before getting out of bounds with 14 seconds remaining.

Prescott took off on a designed run and slid at the end of a 17-yard run, with about eight seconds to go. Umpire Ramon George bumped Prescott trying to set the spot. The snap from the San Francisco 24 came after the clock expired.

Dallas coach Mike McCarthy suggested Prescott was slowed by the collision with George, and that a sideline official assured him the play was being reviewed.

“The communication that I was given on the sideline was they were reviewing it,” McCarthy said. “They were going to put time back on the clock. And the next thing I know, they’re running off the field.”

Players from both teams streamed onto the field immediately after Prescott took the snap and spiked the ball, and many did a U-turn as officials discussed the play. Then referee Alex Kemp announced the game was over.

“It was like the whole day, it really was,” Garoppolo said of the final sequence. “It was a dogfight, hell of an atmosphere out here. I mean, the fans were nuts. It was everything we thought it was going to be. It was fun.”

Kemp said in a pool report George was trailing the play at a proper distance and acted appropriately to get the ball spotted correctly. The umpire has to touch the ball before another play can happen.

Kemp said the decision that the snap came after the clock had expired was made on the field, not on a replay assist from New York.

“The umpire was simply spotting the ball properly,” Kemp said. “He collided with the players as he was setting the ball because he was moving it to the proper spot.”

The 49ers were in control in the fourth quarter, but not leaning on the running game they figured could carry them to a win when Garoppolo threw an interception to Anthony Brown that set up Prescott’s 7-yard scoring run.

Garoppolo’s mistake wasn’t long after Prescott was picked off at the Dallas 26 by K’Waun Williams and Samuel ran untouched on a cutback up the middle to the end zone on the next play for a 23-7 lead.

San Francisco lost star pass rusher Nick Bosa to a concussion just before halftime when he was crunched in the head and neck area by teammate D.J. Jones. But the 49ers kept enough pressure on Prescott, finishing with five sacks while holding the NFL’s No. 1 offense to 307 yards.

“Guys just stepping up big in big key situations, that’s really what it was all day,” Garoppolo said. “Early on, we got it rolling with the offense and the defense just throughout the entire day. We got some dogs on our defense, man. It’s fun.”

San Francisco scored on its first four possessions, but three times settled for field goals from Robbie Gould to help keep the Cowboys close.

Rookie Elijah Mitchell ran for 96 yards and the game’s first points on a 4-yard run. Samuel had 72 yards on 10 carries and three catches for 38 yards.

As was the case most of the season, Dallas couldn’t get the running game going with Ezekiel Elliott. The two-time rushing champion had 31 yards on 12 carries as the Cowboys were outgained 169-77 on the ground.

Prescott was 23 of 43 for 254 yards. Garoppolo, whose future with the 49ers beyond this season is uncertain with rookie Trey Lance waiting his turn, was 16 of 25 for 172 yards.

Dallas was down 13-0 when Prescott threw a 20-yard touchdown pass to Amari Cooper. The Cowboys had the final possession of the first half and the opening drive of the second half to get closer and couldn’t.

“The team should not have been in a position to make that last play be something controversial,” Dallas owner Jerry Jones said. “I’m not going to make it bigger than it is.”

The talk shows will do that for him.

MONEY KICKER

Gould is now the career leader for makes in the playoffs without a miss at 18. Two of his field goals were from at least 50 yards (53, 52).

INJURIES

Concussions almost sidelined the best pass rushers for both teams. Before Bosa was injured, standout Dallas rookie Micah Parsons was evaluated for a concussion in the first quarter. He quickly returned.

UP NEXT

49ers: Head to Green Bay to take on Aaron Rodgers and the Packers in the divisional round next Saturday night.

Cowboys: Season ends with the first wild-card loss in three playoff trips for Prescott.

___

More AP NFL: https://apnews.com/hub/nfl and https://apnews.com/hub/pro-32 and https://twitter.com/AP_NFL