Jeff Bezos’s own space company Blue Origin was also in the running to sign the contract with NASA. However, the US space agency decided to go for Mr Musk’s company, The Washington Post revealed.
The planned spacecraft will eventually send a crewed mission to the Moon in 2024.
Blue Origin, SpaceX and another US technology company, Dynetics, were all given contracts to build a spacecraft for the lunar mission.
However, NASA is reportedly giving SpaceX a $2.9billion deal to build the space technology.
Taking to Twitter on Friday evening, SpaceX confirmed their Starship will send the first humans to the moon since the Apollo missions between 1969 and 1972.
The tweet read: “NASA has selected Starship to land the first astronauts on the lunar surface since the Apollo program!
“We are humbled to help @NASAArtemis usher in a new era of human space exploration.”
The first commercial human lander on the moon hopes to take two American astronauts.
NASA said it plans to make history by having at least one female astronaut in its mission who would be the first woman on the Moon.
Kathy Lueders, NASA’s associate administrator for Human Explorations and Operations Mission Directorate, said in a statement: “With this award, NASA and our partners will complete the first crewed demonstration mission to the surface of the Moon in the 21st century as the agency takes a step forward for women’s equality and long-term deep space exploration.
“This critical step puts humanity on a path to sustainable lunar exploration and keeps our eyes on missions farther into the solar system, including Mars.”
The last crewed mission to the moon took place in 1972 under the Apollo program.
Lisa Watson-Morgan, program manager for HLS at NASA’s Marshall Space Flight Center in Alabama, added: “During the Apollo program, we proved that it is possible to do the seemingly impossible: land humans on the Moon.
“By taking a collaborative approach in working with industry while leveraging NASA’s proven technical expertise and capabilities, we will return American astronauts to the Moon’s surface once again, this time to explore new areas for longer periods of time.”
Mr Bezos recently stepped down as chief executive of his company Amazon to focus on other projects including Blue Origin.
Analysts believe Blue Origin has struggled against its younger rival, SpaceX.
Mr Musk’s space company has become more popular than the two and has found success with its innovative reusable rockets.
The Tesla CEO said the reusable technology will drastically cut down the cost of space travel.
SpaceX’s Starship is intended to evolve into a fully reusable launch and landing system designed for travel to the Moon, Mars and other destinations, according to NASA.