Parshat Bimidbar repeats several times that Moshe did not count the Leviim along with the rest of Bnei Yisrael. "Only the tribe of Levi you should not count...with Bnei Yisrael" (א:מט), "But the Leviim according to their fathers were not to be counted among them" (א:מז), and "and the Leviim were not counted among Bnei Yisrael, just as Hashem commanded" (ב:לג). Why was this so?
In answering this question, Rashi (on מט:א) says, "the legion of the king deserves to be counted by itself." His second answer is that Hashem foresaw that a decree would be made against all those counted, who were 02 years and older, that they would die in the desert. Therefore, Hashem said do not let the Leviim be included in those being counted now, who are going to die, because they are mine and they did not sin by the incident of the golden calf.
Another question we can ask is that Bnei Yisrael's dying in the desert was as a result of accepting the negative reports of the spies about Eretz Yisrael, not because of the golden calf. Why then, would the Leviim be excluded from the count, if they were part of the Meraglim and destined to the same fate as the rest of the people? The Siftei Chachamim answers that although the Leviim did accept the negative reports of the spies, they did not sin by the golden calf. The decree of dying in the desert was for both sins. Since the Leviim only committed one, they were not condemned to die in the desert.
The Ramban explains the progression of commands regarding the counting of the Leviim. He explains that first Hashem gave a list of all the names of the twelve heads of every tribe. This helped Moshe and Aharon in the counting of the tribes of Yisrael (א:ח-טו). This served as a clarification for Moshe because missing in the list was the head of the tribe of Levi. Therefore Moshe did not include the tribe of Levi in the census that was conducted of all the remaining tribes. When Moshe concluded his counting of all twelve tribes, he was not sure how to act with the Leviim. Hashem therefore told him not to count the Leviim from the age of twenty, but to count them separately, "from one month and older as they are appointed" (ג:טו).