It was also the first time that Ali, who converted to Islam 17 years ago, had been nominated for an Oscar.
In an emotional speech, the actor thanked his "wonderful" teachers and his wife, who had given birth to their daughter just days earlier.
Moonlight also picked up the award for best picture.
"I'm so blessed to have had an opportunity. It was such a wonderful experience," Ali said, as he thanked the film's director Barry Jenkins and Tarell Alvin McCraney, the writer of the play that Moonlight was based on.
"I also want to thank my wife, we just had our daughter four days ago, so I just want to thank her for being such a soldier through this process and really carrying me through it all," he added.
The independent film tells the story of Chiron, a young gay African-American boy coming to terms with his sexuality as he grows up in a tough Florida neighbourhood.
Ali, who was born Mahershalalhashbaz Gilmore, won the best supporting actor award for his part as drug dealer Juan, who acts as a father figure to Chiron.
In a bizarre turn of events in Los Angeles, the night ended with the wrong film - La La Land - initially being announced as the winner of best picture.
Ali said that the announcement "threw me a bit". "I don't want to go up there and take anything from someone," he said, adding: "It's very hard to feel joy in a moment like that."
Ali, whose mother is a Christian minister, converted to Islam in 1999 and joined the minority Ahmadiyya Community in 2001. He said that he and his mother put aside the differences in their beliefs, which are "not that important".
In January, Ali delivered a powerful speech at the Screen Actors Guild Awards, explaining how the film Moonlight demonstrates what can happen when people are persecuted.
"What I learned from working on Moonlight is that we see what happens when we persecute people, they fold into themselves," he said.
"And what I was so grateful [for] about Juan was playing a gentleman who saw a young man folding into himself as a result of the persecution of his community and taking that opportunity to uplift him and tell him he mattered, that he was okay. And accept him. I hope that we do a better job of that."