Because both companies realised their fans would love to see Sonic and Mario in the same game — and they were right.
The collaboration began with the Mario & Sonic at the Olympic Games series, which turned a decades-old console rivalry into a friendly sports competition. Sega and Nintendo had also grown less like direct rivals by then, with Sega having left the hardware business, which made a partnership far easier to arrange.
The formula proved popular enough to span several Olympics-themed entries.