In the book, Sauron never imprisoned Gandalf (so the answer is, because that would be detrimental to Peter Jackson's prequels plot):
In The Hobbit's time (or before), they met twice, first in 2063 when Gandalf went to Dol Guldur and Sauron (then known as Necromancer) fled as he wasn't strong enough yet (this is not covered in "The Hobbit", but in "The Lord of the Rings", Appendix B, "The Third Age", despite having happened during when Hobbit was happening).
2063 - Gandalf goes to Dol Guldur. Sauron retreats and hides in the East...
Second time, was during the quest to Erebor, the White Council attacks Dol Guldur where Saruman returned again. He "flees" again (same source).
2850 - Gandalf again enters Dol Guldur, and discovers that its master is indeed Sauron. who is gathering all the Rings and seeking for news of the One, and of Isildur's Heir. He finds Thráin and receives the key of Erebor. Thráin dies in Dol Guldur.
2941 - ... The White Council meets; Saruman agrees to an attack on Dol Guldur, since he now wishes to prevent Sauron from searching the River. Sauron having made his plans abandons Dol Guldur.
In addition, Gandalf recounts how the latter events at Dol Guldur happened during the Council of Elrond:
But Saruman has long studied the arts of the Enemy himself, and thus we have often been able to forestall him. It was by the devices of Saruman that we drove him from Dol Guldur. It might be that he had found some weapons that would drive back the Nine ...
... 'Some here will remember that many years ago I myself dared to pass the doors of the Necromancer in Dol Guldur, and secretly explored his ways, and found thus that our fears were true: he was none other than Sauron, our Enemy of old, at length taking shape and power again. Some, too, will remember also that Saruman dissuaded us from open deeds against him, and for long we watched him only. Yet at last, as his shadow grew, Saruman yielded, and the Council put forth its strength and drove the evil out of Mirkwood and that was in the very year of the finding of this Ring: a strange chance, if chance it was.
'But we were too late, as Elrond foresaw. Sauron also had watched us, and had long prepared against our stroke, governing Mordor from afar through Minas Morgul, where his Nine servants dwelt, until all was ready. Then he gave way before us, but only feigned to flee, and soon after came to the Dark Tower and openly declared himself.
They never directly clashed in LOTR timeline, the only time Gandalf was imprisoned was by Saruman - see below.
Now, in case you meant Saruman and not Sauron, who did, indeed, imprison Gandalf (during LOTR time):
Saruman was hoping to get Gandalf to tell him where the One Ring was.
'He was cold now and perilous. "Yes," he said. "I did not expect you to show wisdom, even in your own behalf; but I gave you the chance of aiding me willingly. and so saving yourself much trouble and pain. The third choice is to stay here, until the end."
' "Until what end?"
' "Until you reveal to me where the One may be found. I may find means to persuade you. Or until it is found in your despite, and the Ruler has time to turn to lighter matters: to devise, say, a fitting reward for the hindrance and insolence of Gandalf the Grey."
(The Fellowship of the Ring, Chapter II: The Council OF Elrond)