Sonic's (in-universe) background is seen in a special comic edition called "The Origin of Sonic"
In short, Sonic was already pretty fast (for a hedgehog) when he first encountered Dr Robotnik.
"The fastest hedgehog on the planet"
Robotnik provided him with "power sneakers" and allowed him to train on his "kinetic gyratoscope", resulting in Sonic being able to run at super-sonic speed.
The comic itself is a retelling of an earlier work; The licensed book Stay Sonic
Partly out of curiosity, and partly to assist Dr K in his research,
Sonic put in several hours a day on a solar-powered treadmill that the
Doc had built for him. The treadmill, or Kinetic Gyratosphere™ to give
it its patented name, was capable of revolving at high speeds. Sonic
protested that it made him feel like a gerbil on steroids. However,
when Kintobor developed special friction-reducing trainers for him he
became more interested. The cutting edge design and classic red
styling hid some state of the art additions including Duratex™ soles
with exceptional grip and specially cushioned odourless inners.
Covering as much ground at the speeds he did frequently left him with
blistered feel. They were also a necessary safety measure because
Sonic started to build up the sort of static that could ignite the
treadmill or shoot sparks into the lab. Gradually Sonic increased his
speed to 200, 400, then 500 mph.
Kintobor torqued up the treadmill every day, pushing it in
20-mile-on-hour increments What hod begun as an idle exercise was
developing into a majorly phenomenal experiment Then one day,
impossibly. Sonic crossed all known limits of mammalian acceleration
and broke the sound barrier The treadmill had been running for
thirty-five minutes and he was little more than a blur when suddenly
there was an enormous bang and a tremendous wind swept all the papers
into the air. Alarmed, Kintobor attempted to slow down the treadmill -
stopping it dead would have had disastrous consequences, probably
sending Sonic into orbit over Mobius. Eventually he managed to gain
control of the machine and gradually reduce Sonic's velocity, but it
took nearly an hour to bring it to a stop
When, finally, Sonic stepped breathlessly out of the treadmill he
wasn't the same hedgehog that had gone in. His physical appearance had
changed dramatically. Instead of his unexceptional, greyish-brown
shade., most of Sonic's body had turned brilliant, cobalt blue, on his
spikes stood straight back in a stiff mohawk
My word!' exclaimed Kintobor, struggling to comprehend what had just
happened I think you've gone blue from the shock waves Not to mention
the Cobalt Effect™' he said
You clocked up 761 miles per hour! You've really earned your name now!
In fact, I should call you SuperSonic - what do you think of that?'
Kintobor cried, dancing around the lab like a demented, er, scientist.
SuperSonic, yeah - radical Cool.' grinned Sonic, examining his glowing
blue body proudly in a mirror, but what about getting some food? All
that running's made me hungry!'
And which in turn is a version of an even earlier backstory given for Sonic in the original "SEGA Sonic bible"
Still, he knew he'd never be a nerd like Dr. K. So partly for
relaxation, and partly for Dr. K's amusement, he logged in several
hours a day on the supersonic treadmill that Kintobor had -built for
him. Slowly, Sonny built his speed to 200 , 400, then 761 m.p.h. --
the speed of sound. Then, impossibly, he crossed all known limits of
acceleration and began running at the speed of light. Alarmed,
Kintobor tried to slow the treadmill. Although he managed to gain
control of the machine and bring his friend's velocity down slowly, a
change had come over Sonny. Instead of his unexceptional,
grayish-brown color, most of his body had turned cobalt blue, and his
quills stood straight back in a stiff mohawk.
"My word!" exclaimed Kintobor, who struggled to comprehend what had
just happened. "1 think you've gone blue from the Advanced Non-
Concussive Cobalt Effect!" he said, helping Sonny from the treadmill.
" 186 thousand miles per second! You're so fast, we should call you
Sonic. SuperSonic, even. How do like that!" he cried, dancing around
the lab like a demented pixie.