PHILADELPHIA -- With the game on the line, the defense held for the Philadelphia Eagles.

Nick Foles threw two touchdown passes, the defense and special teams each scored and the Eagles held on for a 34-28 victory over the St. Louis Rams on Sunday.

The Eagles (4-1) rebounded from a tough loss at San Francisco with a strong all-around performance until the end. Chris Maragos returned a blocked punt for a touchdown, Cedric Thornton recovered a fumble for a score and nearly took another one the distance.

But Austin Davis rallied the Rams (1-3) from a 34-7 deficit with three straight scores, including a 5-yard pass to Brian Quick that cut it to 34-28 with 4:41 left.

The Rams had a chance for the win when they got the ball at their 7 with 1:47 left. Davis threw a 43-yard pass to Brian Quick on first down, but Philadelphia's defense finally made a stop. Davis was under pressure when he threw an incomplete pass on fourth-and-13.

Davis, who began the year as the No. 3 quarterback, finished with 375 yards passing and three TDs in his first game since being elevated to starter for the rest of the season.

The Eagles have scored seven TDs on special teams and defense, including five in the past two games. Meanwhile, Chip Kelly's high-flying, up-tempo offense isn't clicking the way it did last year -- but the team is winning.

Foles tossed a 9-yard pass to Riley Cooper with 30 seconds left in the second quarter, ending a drought of 18 drives and 97:04 without an offensive TD. He connected with Jeremy Maclin on a 24-yard pass for a 34-7 lead in the third.

On St. Louis' first play of the second half, Trent Cole sacked Davis and forced a fumble that Thornton recovered in the end zone for a 27-7 lead. Later in the third quarter, Thornton picked up a fumble after Brandon Graham stripped Zac Stacy and rumbled 40 yards before he was dragged down from behind. That set up Foles' second TD pass.

LeSean McCoy, an All-Pro last year, had 81 yards rushing. He had 39 yards on 29 carries in the previous two games.

The Rams wouldn't go down easily. Benny Cunningham had a 14-yard TD run and Davis fired a 30-yard pass to Kenny Britt to cut it to 34-21 with 9:02 left. Then, a fumble by Foles led to another TD.

But Davis couldn't complete a remarkable comeback.

The Eagles took a 7-0 lead when James Casey blocked a punt, Maragos scooped it up and ran 10 yards for a touchdown. It was the second straight game the Eagles blocked a punt for a score and the first time in team history they've had a TD on special teams in three consecutive games. Chris Polk returned a kickoff 102 yards in a win over Washington in Week 3.

The Eagles' first two drives stalled inside the 10. Cody Parkey kicked field goals of 26 and 27 yards,

After E.J. Gaines intercepted Foles' deep pass at the Rams 16, Davis led them on a long drive. He scrambled backward, avoided a pass rush and threw from the 25, an 8-yard TD pass to Quick in the back of the end zone to cut it to 13-7.

(Copyright 2014 The Associated Press. All rights reserved. This material may not be published, broadcast, rewritten or redistributed.)