The Bees took the lead in the 11th minute, when John Akinde was fouled in the area by Byron Webster.
Last season’s top scorer calmly slotted the ball into the bottom right hand corner of the goal, to the delight of the travelling Barnet faithful.
Martin Allen’s side held on until the 76th minute, but a Steve Morison header beat Graham Stack to force extra time at The Den.
But it was the Bees who find themselves in the hat for the second round, after Andy Yiadom snatched a late winner.
Akinde charged down the right wing in the 102nd minute – cutting into the penalty area with a powerful run.
His pass across goal was coolly finished by Yiadom on the edge of the six yard box.
The victory was no more than Barnet deserved for a high-tempo and energetic performance.
The Bees could have been ahead after just three minutes when Michael Nelson’s long ball was fired over by Akinde.
Stack had to be at his best four minutes later – turning a Lee Martin free-kick around the left hand post at full stretch.
On 25 minutes tricky winger Luke Gambin could have doubled Barnet’s advantage; jinking through the Millwall defence before firing a deflected shot wide of goal.
Gavin Hoyte’s long range effort on the stroke of half time flew wide of the near post, as the Bees went into the interval with the narrowest of leads.
After 60 minutes they almost paid the price for those missed first half chances.
The lively Fred Onyedinma was fouled in the box by Gambin, but Morison’s resulting penalty cannoned back off the cross bar.
Millwall continued to attack after Morison’s equalising header, but Barnet’s determination saw them hold on until the 90 minutes was up.
The Bees looked re-energised during extra time, and Yiadom’s winner leaves fans eagerly awaiting Thursday evening’s round two draw.
BARNET: Stack, Hoyte, Nelson (N’Gala 89’), Dembele, Johnson, Yiadom, Togwell, Champion, Gambin (Vilhete 69’), Weston, (Gash 63’), Akinde.
Unused Substitutes: Stephens, Muggleton, Tomlinson.