Jack Ross' side claimed the lead after just four minutes, with Lee Cattermole stabbing home at the back post after Grant Leadbitter's corner was deflected into his path.
The Black Cats' lead lasted just two minutes, however, with Tom Eaves racing clear of the opposition defence to round goalkeeper Jon McLaughlin and stab home ahead of Tom Flanagan.
Flanagan gained revenge four minutes later, heading home another excellent corner from Leadbitter to ensure the game continued at a breakneck pace.
Neither defence was covering itself in glory and with the Sunderland backline proving every bit as incapable of dealing with set-pieces as the Gillingham back four, the visitors levelled for a second time five minutes before the break.
Sunderland's defenders failed to clear their lines, and while Leonardo Da Silva Lopes' shot was blocked, the ball broke kindly for an unmarked Brandon Hanlan to sweep home.
The game slowed slightly in the second half, but Sunderland's attacking prowess proved decisive as they claimed the lead for a third time in the 66th minute.
Lopes tripped Luke O'Nien as he was shaping to shoot from Reece James' cross, and Will Grigg side-footed home from the penalty spot to claim his first goal since a 4m move from Wigan.
O'Nien won another penalty with 13 minutes left, drawing a foul from Regan Charles-Cook, and this time Aiden McGeady stepped up to convert from the spot.