Look, no question Riku earned the Master title more than Sora did. But there was never any rule that only one of them could pass the exam. In the beginning, it seemed pretty clear that Yen Sid plannedto pass both of them, and he even said at the end that they both deserved to be Masters.
Its hardly Sora's fault that he was specifically targetted by an incredibly complex time travelling plan by the xehanorts that NO ONE was able to predict, and if the situations were reversed, Sora would have done what he had to to save Riku in a heartbeat (see what I did there?) So why did Yen Sid fail Sora. Was it just to add some extra drama, make Riku's success feel a bit more special?