According to local media, videos on social media, and rights groups, thousands of Iranian mourners marched to Mahsa Amini’s hometown early Wednesday to mark 40 days of mourning since her death in police custody, defying a heavy crackdown in response to the protests that have since engulfed the country.

Despite a heavy security presence and threats of imprisonment and death from the authorities, mourners poured into Ms. Amini’s hometown of Saghez in northwestern Iran, arriving in cars, on motorcycles, or on foot. By the afternoon, the protests had spread across the country, attracting tens of thousands, and several videos showed security forces opening fire on protesters in Saghez and Tehran. There were no immediate reports of casualties.

Wednesday is the 40th day of mourning for Ms. Amini according to Islamic tradition. She died in Tehran on September 16 after being arrested by morality police for failing to comply with the hijab law, which requires women to wear a head covering. Her family claims she was in perfect health and died as a result of a head injury sustained while in custody. According to the government, she died of a heart attack.

Outrage over her death sparked more than a month of intense protests across Iran, many of which were led by women and youth and called for the end of the authoritarian clerical system. The protests are largely organic and lack centralized leadership, but prior to Wednesday’s day of mourning, several activist groups — women, university students, and youth — issued statements calling for the public to take to the streets.

According to rights groups, at least 216 people have died in the protest, including 32 children, and thousands have been injured or arrested. Since the protests began, the internet has been severely disrupted in Iran, and the government has blocked popular applications such as WhatsApp and Instagram, making it difficult for Iranians to communicate with the outside world.

Ms. Amini’s brother, Ashkan Amini, who was with her when she was arrested, paid a visit to the cemetery in Saghez with family on Tuesday and posted a video of her grave covered in red rose petals.

Ms. Amini was a member of Iran’s Kurdish minority, and protesters joined in the day of mourning, shouting “Death to the Dictator!” across the heavily Kurdish region that encompassed her hometown. According to rights groups, many storefronts were closed in cities across the country as business owners stayed at home to commemorate the day.

Anticipating a new wave of unrest on Wednesday, the government deployed anti-riot forces and plainclothes police officers on the streets of Saghez the night before, according to images and videos posted on Telegram by Hengaw, the Norway-based Kurdish rights organization.

According to rights groups and videos posted on social media, the security forces, who had reportedly warned Ms. Amini’s family to cancel plans for a gathering at the grave site on Wednesday in Saghez, closed the city’s access roads the night before to prevent mourners from entering.

According to videos posted by Hengaw, clashes erupted on Tuesday between protesters and security forces who had gathered in the city’s main Mother Square, near the Kurd Hotel, where several high-profile soccer stars were staying to attend the ceremony.

Despite the violence, thousands of mourners, many of whom were dressed in black, arrived in Saghez on Wednesday. Videos showed streams of people flocking on foot to the grave site, which is about two miles from downtown Saghez.

In a message posted on Instagram on the eve of Wednesday’s day of mourning, renowned Iranian Kurdish filmmaker Bahman Ghobadi said, “Don’t be silent. Everyone has to come out tomorrow.” Mr. Ghobadi, using Ms. Amini’s Kurdish name Jina, said she represented Iranians fighting autocratic rulers for freedom.

According to residents of Tehran and social media posts and videos, some Iranians skipped work on Wednesday and some parents kept their children at home in a show of solidarity for Ms. Amini.