The Knights rallied from a 21-point deficit in the second half to take the lead, fell behind again and then marched 70 yards in 11 plays to the winning touchdown.
Louisville had its hopes for an unbeaten season come to an end, its final desperate chance for a victory coming up short when Terry Bridgewater's pass from the Central Florida 49 fell incomplete in the end zone as time expired.
The Cardinals are 6-1 for the season and 2-1 in the American Athletic Conference. Central Florida climbed to 5-1 and 2-0.
Bridgewater threw two touchdown passes and the Cardinals owned a 28-7 lead when Louisville's Jamie Quick returned a fumble 30 yards for a score midway through the third period.
Central Florida then scored 24 consecutive points, helped along by a Louisville fumble at its own 17-yard line. Storm Johnson scored on runs of 1 and 20 yards as part of the Knights' rally, finishing with 109 yards on 18 carries, and William Stanback added a touchdown run of 12 yards.
A 34-yard field goal by Shawn Moffit with 7:36 to play boosted the Kings in front 31-28.
Louisville then put together an 83-yard drive to regain the lead, scoring on a 15-yard run by Dominique Brown.
Central Florida came back once more, however, with Bortles completing 6-of-9 passes on the winning touchdown drive. The key throw was a 29-yarder to Josh Reese that took the ball to the Louisville 19 with 1:06 to play.
Bortles completed 21-of-32 passes for 250 yards. Bridgewater suffered only three incompletions in his first 24 passes, but wound up 29-of-38 for 341 yards.