ANNAPOLIS, Md. -- Malcolm Perry's confidence never wavered.
The Navy quarterback had taken a pounding and watched Air Force storm back from a double-digit deficit to take the lead.
In the end, the Midshipmen had some late-game heroics of their own.
Perry had a 4-yard touchdown run with 23 seconds remaining to give the Midshipmen a stunning 34-25 victory over Air Force on Saturday.
"The offense played well," Perry said. "We had a couple of mistakes here and there. But just keeping the mindset that we were going to win the game. Never let it leave our minds."
The Falcons scored 16 consecutive points in the final quarter and took the lead on a 1-yard run by Taven Birdow with 3:15 left.
However, Perry led an 11-play, 75-yard scoring drive to claim the victory in the first leg of the Commander-In-Chief's Trophy. Navy linebacker Tony Brown returned a fumble 8 yards in the closing seconds to round out the scoring.
"We found a way to win," Navy coach Ken Niumatalolo said. "That is a great team win. We went back and forth. We needed to step up and we did."
Perry finished with 111 yards on 23 carries with a pair of touchdowns. He also completed 5 of 7 passes for 144 yards.
A fumble by Navy fullback Nelson Smith gave Air Force the ball on the Midshipmen 37 midway through the fourth quarter. The Falcons capitalized on a 1-yard plunge by quarterback DJ Hammond III that pulled them within 21-19 with 7:38 left in the game.
The Falcons (3-2) then put together a six-play, 72-yard scoring drive, capped by the scoring run by Birdow to take their first lead since the first quarter before the Midshipmen (3-1) rallied.
"We came here and played our hearts out," Falcons defensive tackle Jordan Jackson said. "We did not play good enough to win."
Air Force kicker Jake Koehnke converted all four of his field-goal opportunities, including a career-long 49-yard kick that pulled the Falcons to within 14-9.
Hammond had 71 yards on 12 carries. He also competed 10 of 25 pass attempts for 205 yards with an interception.
Jake Springer managed a key sack in the third quarter that gave Navy a short field, setting up the 20-yard scoring run by Perry that provided a 12-point margin.
The Midshipmen used an almost identical strategy to boost the lead to 14-3. Perry completed a 28-yard pass to Mychal Cooper and Smith capped the drive with a 3-yard scoring run.
"They made a few plays and some really fine plays, too," Air Force coach Troy Calhoun said about Navy.
The Commander-In-Chief's Trophy is presented annually to the winner of the football competition among the three major service academies -- Army West Point, Navy and Air Force. The Black Knights took home the crown the previous two years.
Air Force: The Falcons have not won in Annapolis since 2011. The loss also takes them out of the running for the Commander-In-Chief's Trophy. After averaging 34.6 points over the opening four games, Air Force never fully got into sync against Navy until the final quarter.
Navy: It was an uplifting win for the Midshipmen, who were routed by Air Force 35-7 last season in Colorado Springs. When the Falcons stacked the box, Perry showed he could be effective throwing the ball downfield to set up a couple of scores. The Midshipmen can win their first Commander-In-Chief's Trophy since 2015 with a victory over Army on Dec. 14.
The game had an announced attendance of 37,957 -- the fourth-largest crowd in the history of Navy-Marine Corps Memorial Stadium. It was also the eighth-consecutive time this matchup sold out in Annapolis.
Air Force had just one penalty for 12 yards. Navy was penalized seven times for 52 yards.
Air Force: Hosts Fresno State on Oct. 12
Navy: Visits Tulsa on Oct. 12
More AP college football: http://collegefootball.ap.org and http://www.twitter.com/AP-Top25
Kennesaw State pays former cheerleader Tommia Dean $145K to settle anthem lawsuit
Kennesaw State paid $145,000 to settle a lawsuit against the school filed by former cheerleader Tommia Dean after she was removed from the field for protesting police brutality during the national anthem in 2017.
Washington State extends Mike Leach through 2024
Washington State coach Mike Leach has agreed in principle to a one-year contract extension through the 2024 season, the school announced Thursday.
Does Georgia have enough firepower to take down LSU?
College GameDay analyst Kirk Herbstreit joins SEC Now to discuss what No. 4 Georgia has to do to take down No. 2 LSU in the SEC Championship Game.
Can Georgia's offense smash-mouth its way past LSU in the SEC title game?
The Bulldogs' recipe for success has been built on running the ball and solid defense. How will that stand up in the SEC title game opposite high-scoring LSU?
Best bets for college football's conference title games
Our betting insiders offer their best bets for college football's conference title weekend, including Georgia-LSU.
'Stanford Steve' and 'The Bear': Conference title picks, bets, nuggets
The fellas break down the action from college football conference title weekend with their best bets, parlays, futures and more.