N.C. State drove to the South Carolina 8-yard line, but a fourth-down stop with six seconds left allowed the Gamecocks to hold on in the neutral-site game.
Jake Bentley threw three touchdown passes as South Carolina opened the season with a victory in Charlotte for the second time in two years.
N.C. State quarterback Ryan Finley threw for two touchdowns and rushed for another. He completed 45 of 64 passes for 415 yards, setting a school record for completions in a game.
Bentley ended up 17 of 29 for 215 yards.
With the Gamecocks leading 35-28, N.C. State drove into South Carolina territory but came up short on a fourth-down play near the Gamecocks 30-yard line with 4:39 to play.
After a punt, the Wolfpack had the ball at their 35 with 3:06 left. N.C. State picked up three third-down conversions to reach the South Carolina 8, then threw two incompletions from the 10-yard line on the Wolfpack's last two snaps.
N.C. State held a 504-248 advantage in total yards.
Earlier, the Wolfpack pulled within 35-28 with 12:21 left when Jaylen Samuels made a diving catch in the end zone on a fourth-and-1 play.
The Gamecocks' Samuel, a receiver, returned the game's opening kickoff 97 yards for a touchdown. That led to a high-scoring first quarter, with the game knotted at 14.
Samuel also caught 6- and 39-yard touchdown passes from Bentley.
Running back Rico Dowdle scored twice for the Gamecocks, first on a 34-yard reception in the first quarter and then on a 7-yard run in the third quarter. The latter came after an N.C. State fumble.
N.C. State drove 79 yards in less than two minutes to tie the game with nine seconds left in the first half on Finley's 4-yard pass to Jakobi Meyers.
Nyheim Hines, who scored on a 2-yard run in the first quarter for the Wolfpack, was the game's leading rusher with 81 yards on 15 carries.