CLEVELAND — When the final buzzer sounded and the desperation heave by Jerian Grant had gone safely long, the Kentucky players swarmed off the bench to jump and hug their victorious teammates on the floor, a signal of how close they had come to heartbreak.

It was the kind of celebratory reaction that an underdog usually has after scoring an upset against a heavy favorite in the N.C.A.A. tournament. But this was the other way around.

The mighty Kentucky Wildcats were the strong favorites, an undefeated juggernaut looking to make history with 40 wins and an unblemished record.

Kentucky was thought to be too tall, too deep and too agile to be overcome, even by the plucky Fighting Irish. Notre Dame, the third seed in the Midwest Region, did not feel that way, and certainly did not play like it, either, forcing the top-seeded Wildcats to scratch for survival.

“We were just fighting to stay in the game, to be honest with you,” Kentucky Coach John Calipari said.

The Irish (32-6) gave the Wildcats the scare of their season in a game that came down to the last shot. But the Kentucky machine — perhaps dented and vulnerable — still rolled to its 17th Final Four appearance on Saturday with a harrowing 68-66 victory over a worthy Notre Dame team in the round of 8 at Quicken Loans Arena.

Until the bitter end, Notre Dame’s dream of beating Kentucky and winning a national championship was very much alive. But the 2015 college basketball season will countenance only one dream, and it may yet be bathed in Kentucky blue.

The Notre Dame players gave as much as they took, with two-handed dunks and pick-and-roll plays that seemed to confuse the defensively oriented Wildcats.

But Kentucky improved to 38-0, and it needs two more wins to secure its ninth N.C.A.A. championship and its second under Calipari.

In its previous game, Kentucky had rolled to a 39-point victory over cocky West Virginia. Notre Dame came in far less brash, but still very confident. After all, it had beaten Duke and North Carolina on its way to an Atlantic Coast Conference championship in only its second year in the league. Why not beat Kentucky, especially after the first half ended at 31-31?

“We really thought we had a great chance as the game was going on,” Notre Dame Coach Mike Brey said.

Playing in front of an audience that included LeBron James and J. R. Smith of the Cleveland Cavaliers, as well as many more rushing to television sets across the country as word spread of a potentially enormous upset, the teams went back and forth from the opening tip until the final shot, when the game could have gone either way.

Notre Dame led by 5 points with five minutes to play, and by 2, at 66-64, after Grant’s 3-pointer with 2 minutes 35 seconds to go. An upset, which had seemed so improbable before the game, was now quite possible.

“We had them right where we wanted them,” said Zach Auguste, a junior forward for the Irish.

But Notre Dame would not score again. A minute later, the freshman Karl-Anthony Towns rose in front of the basket and hit to draw Kentucky even, and the Wildcats then forced a shot-clock violation with 33 seconds to play. Towns, a nonfactor in the previous game, scored 17 of his 25 points after halftime to lead the Wildcats.

“In the second half, I just trusted what the coaching staff told me,” Towns said, “and I guess the results speak for themselves.”

On the next possession, with the game still tied at 66-66 and virtually every fan from the floor to the top row standing, Kentucky guard Andrew Harrison waited until there were nine seconds to play, drove into the lane and was fouled by Demetrius Jackson. Harrison coolly hit both free throws with six seconds left.

It was his twin, Aaron, who hit a critical 3-pointer with just over three minutes to play and gave Kentucky its previous lead. Now, thanks to Andrew Harrison’s focus at the line, Kentucky was back in front.

Notre Dame was out of timeouts, having used two just to help the players catch their breath in what they hoped would be the outcome of their lives. So Pat Connaughton, Notre Dame’s senior forward, inbounded to Grant, his fellow senior and close friend.

Grant dribbled as fast as he could but saw no direct path to the rim. Instead, he ran to the left corner, directly in front of the Notre Dame bench, and hoisted a desperation 3-point attempt. If it had gone in, Notre Dame would have scored one of the biggest tournament upsets, up there with Villanova over Georgetown in 1985 and Duke against the unbeaten Nevada-Las Vegas in 1991.

Until the final three possessions, Kentucky could not stop Notre Dame on three consecutive trips down the court. What, then, accounted for the Wildcats’ ability to make a stop at the very end?

“Desperation, probably,” Andrew Harrison said. “We had no choice, or we were going to lose.”

Grant’s shot went long, igniting the Kentucky celebration as the Notre Dame players stood in front of the basket, stunned and disconsolate. A shocking upset that would have sent Notre Dame to only its second Final Four, and its first since 1978, did not materialize.

Instead, Kentucky will go back to the Final Four, in Indianapolis.

“They came right at us,” Calipari said. “They were not afraid; they were confident. We made some plays down the stretch. We don’t make them, they are going to Indy, and we’re going home.”

Auguste scored 20 points for Notre Dame. Grant added 15 points and 6 assists, and Steve Vasturia contributed 16 points. For Grant and Connaughton, whose daring drive and dunk midway through the second half ignited the Irish and their fans, it was the final game.

Brey said that in the locker room after the game, he had told the players: “You know what’s really depressing? We lost the game, but we don’t get to practice tomorrow. I don’t get to be around this group.”

Calipari will have at least one more week to practice with his players, many of whom are expected to move on to the N.B.A. But for now, their focus is to become the first men’s team to go 40-0 and win a title. After Saturday’s narrow escape, it is not as much of a sure thing.

“We know we’re not perfect,” Calipari said. “We’re undefeated, but we’re not perfect. We’re really young, and we showed it tonight a ton.”