Weird, to say the least. Neither of these characters should be fighting each other unless one of the players has only one viable character to use or some janky counterpicking has gone one. Sonic is a polarized character (great matches, horrible matches, very little 50/50s) so he either gets counterpicked or absolutely rocks. Regardless, it does happen occasionally, so as a player of both these characters here's the deal-
From Sonic's perspective:
Mario has a very good aerial game compared to you. Don't challenge him in the air if he's spacing uairs and bairs. Bait/punish/hitconfirm only if you're gonna fight him in the air. His dair is lethal and will cut right through your downB and sideB, both tap and hold versions and he can use it as much as he wants. Beat it by waiting for him to land and grabbing (tapSideB -> JC -> grab if you're tech savvy). You have to time that grab carefully but it's a good grab because Sonic is so fast. The good news is that Mario's KO-off-the-top game is pretty bad, save fair, which he has to set up with a throw or hard read. Mario should not be getting grabs on you, period. You're faster and his grab is garbage (his throws are not... beware...) Fireballs (like any hitstun projectile) are lethal to Sonic. If he gets spammy, revert to type-B Sonic (forget spin to win. space fairs, dairs, nairs, grabs and traditional smash tactics). Off stage, mario is one of the best characters in the game. He has a bag of 4 tricks (1 jump, 1 cape, 1 mario tornado and 1 upB) so don't even bother trying to gimp him unless he's in hitstun and you're finishing a combo with a fair spike. Otherwise, just stay on stage and guard from there. Mario wins this matchup pretty handily- find another character to fight Mario with.
From Mario's point of view:
Sonic is faster than you period, but you're much better equipped than he is. The modern sonic knows about the difference between tapSideB, SideB and DownB and how to vary them to mix you up. Sitting in CC waiting to get hit how you wanna win- it's better to take it to the air and spam fireballs, hitconfirming into nairs, uairs and dairs. Behind shield dair -> utilt works as usual. Cape can stop spin to win, but fireballs will also stop him and set you up for followups. Fireballs, fireballs, fireballs (or pills if you prefer). dSmash is a great tool against Sonic- it comes out fast, it's powerful and it covers both sides. If he's applying pressure along the ground, CC and dsmash; just remember to CC sparingly. Sonic's upB recovery is VERY hard to sweet spot and effectively impossible on some stages (Green Hills, where he'd have to drop into the blast zone to not overshoot the ledge). One well timed cape and he's dead. Sonic's floaty and is easily KOable off the top- dthrow or uthrow (when appropriate %) to fair is viable. Sonic players sometimes have trouble DIing correctly because of how fast paced Sonic's inputs are- take advantage and get that grab -> dthrow/uthrow -> fair. Sonic off stage isn't terribly nasty, but he's not useless. Stay away from the front of him, as his fair is deadly off stage. Mario wins the match with smart projectiles, followups and patient recoveries.