The Oilers erased a 1-0 deficit with the two quick goals, which came at 7:13 and 7:27 into the final period. Foster's score came on a power play and Gagner quickly followed up with his third tally of the season.
Nikolai Khabibulin was strong in goal against his former team, stopping 26-of-27 Chicago shots, including nine in the final period when the Blackhawks swarmed the net seeking the tying goal as time wound down.
The Oilers have won both their visits to United Center this season after losing all four games to the NHL champions last year.
The Blackhawks fell to 8-8-1 with the setback. Chicago got its only goal from ex-Oiler Fernando Pisani and Corey Crawford stopped 24-of-26 shots.