We know at the end of the Hobbit novel that te necromancer is driven from Mirkwood, but little details were offered.
Yes, the Necromancer is driven from Mirkwood, nowhere is Gandalf captured and restrained (and if he were, The Enemy would almost certainly have taken Narya from him).
Besides, per Fellowship, Gandalf could not have encountered the Necromancer in such a way because they were not aware of Sauron’s *secret* activities as he regathered his former strength. In a “face to face” meeting Gandalf would recognize Sauron.
I’ll step off my soapbox, now.