You need the lock-on cartridge, Sonic and Knuckles. It has a slot on top designed to accept another cart: put Sonic 3 on it and you can play as Knuckles through the whole game. The same trick works with Sonic 2.