Full-time score from The Den
Chris Wilder's League Two Cobblers - and keeper Ryan Clarke in particular - had frustrated Neil Harris' charges as the game entered into the final 20 minutes.
But a superb individual effort from O'Brien on 71 minutes was quickly followed by Gregory's second from close range two minutes later and The Lions were through to the last eight in the Southern section of the competition.
There were six changes in all to the side that started against Peterborough at the weekend and not surprisingly it took a while for The Lions to get into their stride in a first-half in which the Cobblers enjoyed more of the possession, but Millwall created the only clear chances.
The woodwork denied O'Brien an opening goal on the quarter-hour mark with the young striker popping up on the left flank, picking up Lee Martin's through ball and taking a touch before slamming his shot onto the post from an acute angle.
At the other end Lawson D'Ath's stinging shot bounced out of Jordan Archer's hands, but the keeper gathered the loose ball and the danger was averted.
Steve Morison was denied by Cobblers keeper Clarke twice in quick succession after he was played in on goal, blocking the frontman's first goalbound effort, then spreading himself brilliantly to clear the follow-up with Martin finally lashing the loose ball over from 12 yards.
Left-back Joe Martin despatched an inviting delivery bang on half-time that Morison met with a firm header, but keeper Clarke was equal to his effort and saved low to his left to send the teams in 0-0 at the break.
The Lions went straight on the attack at the start of the second-half with Morison hooking the ball to strike partner Gregory, but the latter rushed at his shot and thumped high over the bar from 16 yards.
An inspired double substitution by Harris saw Ben Thompson and Fred Onyedinma replace Martin and Jack Powell and the move certainly added impetus and thrust in the final third of the field.
Another great cross from the left from debutant Martin picked out Morison after 67 minutes, and his personal dual with keeper Clarke continued as the latter once again flung himself across goal to keep out the striker's powerful header as it arrowed towards the top corner.
Just four minutes later The Lions finally made the breakthrough - and what a corker it was too. O'Brien worked his magic and cut in from the left, shaking off a couple of defenders before lashing into the bottom corner from 20 yards for his first ever senior goal at The Den.
The fans were celebrating again just two minutes later when Morison latched onto Gregory's pass before crossing for his strike partner to chest in from just four yards out.
Northampton nearly reduced the arrears on 79 minutes when D'Ath's drive smashed against the upright and two minutes later Dominic Calvert-Lewin's header was tipped away by Archer as the visitors tried to work up a head of steam.
Thereafter it was all Millwall, however, and on another day Harris' men could have rattled in a couple more. Morison broke into the penalty area and was yet again thwarted by Clarke, whilst Onyedinma's jabbed effort towards goal was pushed around the post by the keeper who more than earned his crust.
Bookings: L. Martin (35mins), Gregory (60mins) J. Martin (78mins)
Scorers: O'Brien (71mins), Gregory (73mins)
Northampton Town: Clarke, Taylor (Hoskins, 66mins), Cresswell, D'Ath, Buchanan, Watson, Calvert-Lewin, O'Toole, Hackett (Adams, 66mins), Furlong, Brisley. Subs not used: Smith, Diamond, Lelan.
Booking: Calvert-Lewin (66mins)
Ref: I. Williamson
Att: 2,254 (away 212)