Mohammad Haque was gazing out the window during math class on the fourth day of his senior year at Stuyvesant HS when he saw a plane fly directly into the North Tower of the World Trade Center.

“I was yelling and saying, ‘Oh, my God’ … We were all looking out at this ball of fire,” Haque, a now-35-year-old Queens dentist, recalls in the new documentary “In the Shadow of the Towers: Stuyvesant High on 9/11,” airing at 9 p.m. Wednesday on HBO. “We realized we were watching people jump from the tower down to whatever they thought at the moment was their best option.”

The city’s most competitive public high school, Stuyvesant is a hulking building at the intersection of Chambers and West streets along the West Side Highway, just four blocks north of Ground Zero. Its proximity meant that, as mere teenagers, Haque, then 17, and his 3,300 classmates were front-line witnesses to the horrific tragedies of Sept. 11.

The stories of eight Stuyvesant students form the narrative of the 30-minute film by director Amy Schatz, whose past work aims to interpret fraught topics — the Holocaust, the Parkland school shooting and endangered animals — for young viewers. She was initially commissioned to make an educational tool for children about that fateful day — “What Happened on September 11?,” airing at 6 p.m. Wednesday on HBO — but she was so moved by the Stuyvesant alums’ tales that she also made a documentary for adults.

“I was struck by the immediacy and vibrancy and rawness after all these years, because they were so close,” Schatz tells The Post. “I just felt heartbreak and loss and friendship, and it took my breath away how vivid it was after 18 years.”

Carlos Williams, then a 14-year-old freshman from Bed-Stuy, remembers fleeing north along the Hudson River with scores of friends after the first tower fell — making the school shake and lights go out. Stuyvesant was officially evacuated, and he says it was like a “war zone” during the exodus. A classmate who wore a hijab was verbally assaulted, other students told Schatz, but peers protected her.

‘We were all looking out at this ball of fire.’

In the film, the students describe the shock, numbness and surrealism of Sept. 11, the isolation of watching the news on television while Stuyvesant was closed for a month, the charred smell of the rubble and ID checks when the doors reopened, and the ironclad bonds that cemented classmates.

“I think people are just starting to be able to take care of themselves mentally. I definitely had the sense of, ‘I’m OK. I didn’t lose any loved ones. I’m fine,’ ” says Williams, now 32 and a branding agency CEO who splits his time between Seattle and New York. “I only saw mental health professionals over the last two or three years. I found that was not very uncommon. I talk about it with friends from homeroom more now than when I was 15. Now [I] sit back and say, ‘This was a huge turning point.’ ”

In 2002, former President Bill Clinton spoke at Stuyvesant’s graduation, praising the teens’ “courage, determination and spirit” after enduring “something someone your age should never have to face.”

The Financial District building where Jersey City resident Ilya Feldsherov, 35, now works as an attorney has views of both his former high school and the World Trade Center site, including the pair of memorial fountains in the former Twin Tower foundations. Every morning, he spends a few minutes staring at the footprints of the buildings and his alma mater.

“It felt unreal,” says Feldsherov, the son of Ukrainian immigrants who was a 17-year-old senior living in East New York on Sept. 11. “The reason it felt so personal was that I came here as an immigrant, and New York was a city where your dreams were possible. And then it was attacked, so the American dream had been attacked.”

Haque, who now lives in Roslyn, NY, walked back to his home in Jamaica via the 59th Street Bridge. His Bangladeshi father, also a dentist, had been told by his assistant that Stuyvesant had exploded; he sobbed in relief when Haque borrowed a friend’s cellphone and managed to reach him. But later that day, the family found out that Haque’s uncle Mohammed Shajahan, who worked on the upper floors of the North Tower as a computer administrator for Marsh & McLennan, had died.

In the aftermath of Sept. 11, students also grappled with the complex issues of hate crimes and minority discrimination.

“My parents didn’t let me out of the house,” Haque says in the film. “They were afraid that some random person with some preconceived notion or idea would take out their anger or frustrations … on some kid they see who looks like [someone they fear].”

Even so, Haque remembers reassurance from the Stuy community amid the terror. “As a person of color and a Muslim-American … I knew there was fear and hate and bigotry everywhere,” he tells The Post. “But within my own class of 800 people, I felt so at home and protected and safe and loved.”

“It’s one of the reasons I’ve never left New York,” says Haque. “I’ve always had crazy NYC pride, mainly because of what I went through on that day.”