We don't know the counterfactual so that's difficult to answer.
I think the truth is that these two companies were a great fit for Musk's style in particular, not that Musk is necessarily a great leader.
What I mean is that Musk is reportedly overbearing and extremely demanding as a leader, and if you don't deliver he will fire you without hesitation. In most organizations that would quickly lead to morale and staffing quality problems, but SpaceX and Tesla worked on really sexy problems, so people were willing to put up with a lot of bullshit and abuse in exchange for getting to work on those problems.
Based on how Elon seems to be running Twitter, it doesn't seem like he ran SpaceX and Twitter in that fashion with intent because he realized he could get away with demanding so much from his employees. Rather it's just how he operates -- meaning that he got lucky, yet again.
But we'll see, right? Twitter is basically the opposite of a sexy problem, so if he turns that around then maybe he really is a talented leader.