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
Notre Dame sellout streak expected to end at 273 games
Notre Dame's run of 273 home sellouts, which ranks second to Nebraska (373) among active streaks, is expected to come to an end against Navy on Saturday, the school said.
Robert Mathis, Erik Williams selected for Black College Hall of Fame
Robert Mathis of Alabama A&M, Erik Williams of Central State University and coach Joe Taylor have been selected for induction into the Black College Football Hall of Fame.
Inside the Clemson Tigers' security detail protecting Trevor Lawrence
Clemson's star quarterback needs some assistance keeping fans from getting too close as the school tries to balance celebrity with the program's traditions.
College Football Playoff: The best, worst and most realistic conference scenarios
There's still plenty of room for movement among teams fighting for a playoff spot in Week 12. See what could be ahead for Oregon and other programs still in the hunt.
'Stanford Steve' and 'The Bear': Week 12 college football picks, bets, nuggets
What's in store for this weekend? The fellas break down Week 12 college football with their best bets, games to stay away from and more.
Best bets for Week 12 college football games
Looking for the best bets in Week 12 of the college football season? Our betting insiders offer their picks for the biggest games.