Of course, this being my own version of Metal, he has a slightly different story.
Metal Sonic was indeed smashed to pieces by running into a wall when he first clashed with Sonic in CD, but the way I see it, the heavily damaged Metal then crawled his way off Little Planet, following Sonic, wanting to have a rematch. In the time since the fight, Sonic took down the Death Egg, so by the time Metal arrived, he found the crashed fortress with most of it's robots intact.
Working his way into Robotnik's assembly lines, he stole parts of other Metal Sonics in order to repair himself- the arm and torso of Silver Sonic (Sonic 2), the leg and energy core of Mecha Sonic (Sonic 3&K) and the prototype leg of Mecha Sonic 2 (the cancelled Sonic X-treme).
With his body in working order, it was time for a face off…!