A 15-foot bunt single threatened to give South Carolina a sweep of Saturday's Southeastern Conference doubleheader against host Kentucky.
But a two-run homer by freshman Andy Burns with one out in the bottom of the ninth gave the Wildcats a 5-4 victory after the Gamecocks survived a UK comeback to win the opener 20-19.
"We had 11 homers in the first game, so I thought there might be one more left," UK Coach Gary Henderson said. "We didn't like how they scored in the ninth, but we were just chasing a run."
South Carolina broke a 3-3 tie on Scott Wingo's bunt single, scoring Kyle Enders from third.
Sign Up and Save
Get six months of free digital access to the Lexington Herald-Leader
With one out in the bottom of the ninth, UK's Chris Wade was hit by a pitch by closer Alex Farotto, who saved the opener.
Burns lifted a fly to right, and the Gamecocks' Jackie Bradley Jr. kept drifting back.
"I definitely wasn't sure," Burns said. "It carried, and I saw him get to the wall. It kept going over then."
"The way the ball was flying, I thought it had a shot," Henderson said. "When (Bradley) went to the wall, I thought maybe.
"It was a great win. I was disappointed how the first game ended, but I liked the way we fought. We competed all day."
The series concludes at 1 p.m. Sunday. Freshman Alex Meyer (1-1) will pitch for the Cats (16-8, 4-4 SEC) against senior Blake Cooper (3-2) and the Gamecocks (17-7, 3-5).
UK one run short
At last, at the end of a 12-run rally covering its last three at-bats, Kentucky got the tying run to the plate in the opener.
After Gunner Glad's two-out, two-run homer brought the Cats within one, South Carolina needed an over-the-shoulder catch by shortstop Bobby Haney in short center field to end the opener four hours and 13 minutes after it began. (The nightcap went 3:14.)
The Gamecocks led 9-0 after two innings and 13-2 after four. Their biggest lead was 18-5 after 5½ innings, and the visitors were ahead 20-7 after 6½ innings.
Kentucky scored six runs in the seventh and four in the eighth to edge closer.
Highlights included the 11 home runs, 43 hits, 17 runs allowed by the starting pitchers and only two errors in the marathon.
Individual standouts in the opener included:
■ UK junior Glad with his three homers, five RBI and five runs and Chris Bisson's four hits and four RBI. Spencer Korus entered the game as a pinch runner and wound up with two hits and four RBI, including a three-run shot in the seventh inning as the Cats launched their comeback.
■ The Gamecocks' Nick Ebert, whose only hit was a grand slam; Andrew Crisp, Justin Dalles, Bradley and Whit Merrifield with four hits each; and Parker Bangs and Scott Wingo with three RBI each.
UK starter James Paxton's ERA went from 3.30 to 5.62 for his two innings of work. Carolina starter Sam Dyson couldn't handle the 20-7 lead and left with two outs in the seventh when he hit a batter after getting two strikes. The visiting bullpen gave up six runs after that, including Glad's second homer and a three-run shot by Chris McClendon.
Despite the loss, Burns called the opener a mental victory after the Cats' comeback.
"We needed and wanted the win," he said.
Burns and his teammates had to wait until the nightcap.
UK's victory ended a four-game losing streak, including two non-conference defeats in the middle of the week.
After the slugfest, the teams settled into a pitching duel between the Cats' Chris Rusin and Carolina's Nolan Belcher.
Kentucky tied the score 3-3 in the eighth before Wingo's successful bunt left the visitors three outs from a sweep. But Burns gave the Cats their third walk-off victory of the season. All three home runs came on the first pitch.
Rusin was lifted with two outs in the ninth. He is four outs shy of pitching complete games in his last four starts.
The nightcap was delayed 17 minutes when plate umpire Kenneth Couch was hit in the face when the bat slipped out of McClendon's hands in the bottom of the eighth.
Couch was feeling woozy and went to University Hospital. His status for the series finale was unknown.