SANTA CLARA, Calif. -- An off-balance Colin Kaepernick improvised, threw across his body and across the field, finding Frank Gore open near the opposite sideline more than 20 yards away.

Gore bailed out a broken play and helped get the San Francisco 49ers back on track with a big day.

He caught that career-best 55-yard touchdown and ran for 119 yards in his first 100-yard game of 2014, leading San Francisco past Philadelphia 26-21 on Sunday to hand the Eagles their first loss.

"I don't think I've ever had one quite like that," Kaepernick said. "It was a great job by Frank."

"Great job by Kap," Gore offered, standing next to his quarterback on the postgame podium.

Coach Jim Harbaugh didn't see Gore open and had no idea where Kaepernick was going on that play.

"Kap did a great job keeping his eyes up field. I had no idea. When he stopped, pulled it up and started to throw, I didn't know where he was going with the ball," Harbaugh said. "The speed, the angle, the way he got into the end zone was something. I didn't think he was going to get it in."

Regardless, the 49ers got Gore involved in the offense again, and it returned them to their winning ways. Hard-nosed defense helped, too.

At least for one Sunday, it seems the Niners reclaimed their offensive identity and defensive swagger.

The 49ers (2-2) avoided their first three-game losing streak under Harbaugh, containing the league's top passer.

Nick Foles threw two incomplete passes from the 1 in the waning moments after gaining six first downs on a lengthy drive, more than the five the Eagles managed before that.

"We just didn't execute, it's as simple as that," Foles said. "I'm not going to break it down or anything."

Foles completed a 22-yard pass to Jeremy Maclin on the right sideline on that late drive. The 49ers challenged whether he had possession, but the play stood, costing the 49ers their final timeout.

The Eagles couldn't pull off the second-half magic that had carried them in their three wins.

On a day the 49ers gave up big plays on special teams, Gore delivered. Kaepernick also threw a touchdown pass to Stevie Johnson and Phil Dawson kicked four field goals -- two of at least 46 yards. Kaepernick finished 17 for 30 for 218 yards, two TDs and four sacks.

Darren Sproles had a career-best 82-yard punt return for a touchdown, Malcolm Jenkins ran an interception 53 yards for a score for the Eagles (3-1). Brad Smith recovered Trey Burton's blocked punt against Andy Lee for a TD.

The 49ers produced in the second half for the first time this season to give Harbaugh a win against former Pac-10 foe Chip Kelly.

Antoine Bethea intercepted his first pass with the 49ers and forced a fumble, then the secondary kept the pressure on in the red zone. Perrish Cox made an interception with 41 seconds left to seal it.

Gore's score on the first play of the second quarter was the longest TD catch by a running back this season.

"It was an improvisational play, but that's one of his strengths," Eagles defensive coordinator Bill Davis said of Kaepernick. "It's the skill set that he possesses. He's able to make plays like that happen."

But on San Francisco's next series, Kaepernick threw right to Jenkins and he broke three tackles on the way to the end zone.

The 49ers won their first game at new Levi's Stadium, where thousands of fans skipped out at halftime.

San Francisco outgained the Eagles 232-73 in the first half, but still trailed 21-13. Then Johnson tiptoed both feet inbounds inside the left pylon in the third quarter for a 12-yard catch, the first second-half TD of the season for San Francisco after it had been outscored 52-3 after halftime in the first three games.

Dawson kicked a 51-yarder before halftime, but the NFL's most-penalized team hurt itself with seven more flags for 60 yards in the first half and 10 total for 80 yards -- but none on defense. The Eagles also had 10 penalties for 70.

San Francisco right tackle Anthony Davis made his season debut but went down on the final play of the first quarter when Kaepernick took a sack right into the back of Davis' left knee.

Tight end Vernon Davis was sidelined with a back injury early in the third quarter and he was still experiencing spasms after the game, though an X-ray was negative. Davis missed last Sunday's loss at Arizona with a hurting left ankle.

Foles was 21 for 43 for 195 yards and two interceptions.

Game notes

Foles missed a chance to join Peyton Manning, Steve Young, Kurt Warner and Drew Brees as the only QBs to open the season with four straight 300-yard passing games. ... 49ers Anquan Boldin and Michael Crabtree had five catches each.