The team had raced at Le Mans since 2012 with a very competitive auto, yet had never managed to score an outright victory.
Toyota's closest challengers were the two Rebellion Racing cars which finished 12 laps back.
Alonso is bidding to match British driver Graham Hill's feat of completing the Triple Crown, including wins at the Monaco Grand Prix and Indianapolis 500.
In the 16th hour of the race, Nakajima retook the lead and the number eight auto was in control for the duration.
It seemed to be a case of "oh no, not again" for Toyota when Kamui Kobayashi in the number 7 vehicle slowed down to 80km/h with what seemed like a fueling problem heading towards the end of the race.
Morocco v Iran 15 June 2018
The match in itself had far from the smoothness of the first half, with injuries and substitutions punctuating the entire half. But, despite Morocco having 68% of possession and 13 shots, Iran picked up the victory.
Alonso shared the number eight Toyota TS050 hybrid with Swiss driver Sebastien Buemi and Japan's Kazuki Nakajima, who secured pole position and took the chequered flag. It later turnout out that Kamui Kobayashi had simply forgotten to pit and was asked to conserve fuel to make it through the lap.
Fernando Alonso has always been regarded as one of the all-time great racing drivers and the best along with Lewis Hamilton in the current era of F1, but he has found success hard to come by in grands prix in recent years.
Toyota is the only manufacturer with a team in the World Endurance Championships after the withdrawals of Audi and Porsche in the last two years. However, as time has shown, having the fastest vehicle is no guarantee of success.
If it was a genuine fight, at least for the first two-thirds of the race, Alonso's apparently impressive multiple stints in the middle of the night, when he closed a two-minute lead to virtually nothing, will come to be seen as playing a decisive role in the victory for the number eight vehicle.
But Toyota insisted that the two cars went into the race with equal chances of winning. "I don't think too much about how competitive we will be next year as it is impossible to predict: It is just about the sport".