SANTA CLARA, Calif. (AP) Another miscommunication almost led to another game-altering interception for Jimmy Garoppolo.

Only this time, the San Francisco 49ers got bailed out by a flag and held on to beat the Detroit Lions.

Garoppolo threw two touchdown passes and caught a break when a defensive holding call negated a late interception as the 49ers held on to beat the Lions 30-27 on Sunday.

''Very happy,'' Garoppolo said of his emotions after realizing a penalty wiped out his mistake. ''I didn't see what happened on that with the flag and everything but very happy.''

The big play came after Detroit (0-2) had cut San Francisco's 30-13 lead to three points thanks to two fourth-quarter touchdown passes by Matthew Stafford. Then with the 49ers (1-1) facing a third-and-2 near midfield, Garoppolo threw a quick out to Matt Breida. Tracy Walker stepped in front and intercepted the pass, returning it to the San Francisco 7.

But Quandre Diggs was called for holding George Kittle on the other side of the field, giving the 49ers a reprieve.

''I didn't see it, but it doesn't matter,'' Detroit coach Matt Patricia said. ''That's the way the game is being called all the way across the board. We just can't do it, make plays and then the penalties.''

Last week a wrong pattern run by Kendrick Bourne led to a pick-six by Garoppolo, who threw three interceptions in a 24-16 loss at Minnesota that gave him his first loss after winning his first seven career starts.

Garoppolo threw a 4-yard TD pass to Bourne in the second quarter Sunday and an 11-yarder to Garrett Celek in the third to hand Patricia, his old friend from their New England days, a second straight loss to begin his tenure as coach of the Lions. Garoppolo finished 18 for 26 for 206 yards, but was sacked six times.

Breida did much of the rest by rushing for 138 yards and breaking the game open with a 66-yard touchdown run in the third quarter with help from impressive downfield blocking by receiver Pierre Garcon. It was the longest run for San Francisco since Colin Kaepernick scored on a 90-yard run against the Chargers on Dec. 20, 2014.

''It's like backyard football,'' Breida said. ''You see someone break a long run, see someone out there running, you have to block someone. That's what he did.''

Stafford was much sharper than he was last week for the Lions when he threw four interceptions. He went 34 for 53 for 347 yards, three touchdowns and no interceptions but it wasn't enough.

He threw TD passes in the fourth quarter to Marvin Jones Jr. and Michael Roberts but was unable to lead another scoring drive after taking over at the 16 with 1:08 to play. He threw an incomplete pass to Theo Riddick on fourth-and-2 from his 39 with 14 seconds left, ending the comeback attempt.

''We just have to make a few more plays, whether it be earlier in the game or later,'' Stafford said. ''There's a million plays throughout the game we can clean up and be better, and it starts with me.''

EJECTION

Detroit running back LeGarrette Blount was ejected in the third quarter. Blount took exception to Elijah Lee pushing Stafford as he stepped out of bounds. Blount was standing on the sideline right in front of the play and went on the field and pushed Lee. Blount was assessed a personal foul and ejected.

THIRD-QUARTER WOES

The Lions once again struggled coming out of halftime. Detroit was outscored 31-7 in the third quarter in a loss to the Jets last week and then 14-3 by the 49ers in this game.

RECORD-SETTING

The 49ers had three drives stall after getting inside the red zone in part because Detroit sacked Garoppolo three times on plays that started inside the 20. That led to three field goals for Robbie Gould, giving him 29 straight makes, two more than Phil Dawson's franchise record set in 2013.

GOOD RETURNS

Rookie D.J. Reed started the second half with a long kickoff return that initially appeared to go for a touchdown. But he was called for a facemask penalty while giving a stiff arm and was only credited with a 90-yard return, the longest for the Niners since Kyle Williams had a 94-yarder in 2012. San Francisco still managed to get a TD with a 26-yard drive capped by Garoppolo's pass to Celek.

INJURIES

Lions CB Darius Slay left with a concussion in the third quarter. ... 49ers S Jaquiski Tartt left in the second half with a shoulder injury.

ANTHEM

Injured 49ers WR Marquise Goodwin raised his fist during the national anthem. No other player had a visible protest.

UP NEXT

Lions: Host New England on Sunday night.

49ers: Visit Kansas City on Sunday.

---

More AP NFL: https://apnews.com/tag/NFL and https://twitter.com/AP-NFL

Copyright 2018 by AP. Any commercial use or distribution without the express written consent of AP is strictly prohibited.