Electronic device searches were already on the rise under the Obama administration, with the number of travelers whose devices were searched more than doubling from 2015 to 2016. The monthly total of searches since Mr. Trump took office has remained roughly at the level that occurred during the last months of the Obama administration.

Ms. Alaoui, who drove to the border in early February, had intended to spend the day in Burlington shopping for toys for her 5-year-old son, who had just finished three months of chemotherapy.

On past trips, she said, she had spent about two minutes at the border before being waved through. This time, she and a cousin were questioned and held for four hours while their phones and car were searched.

After telling her to write down the pass code to her phone, a customs officer asked where she had been born (Morocco), how long she had lived in Canada (more than 20 years) and if she was a practicing Muslim (yes).

The interrogation quickly veered further toward religious questions, Ms. Alaoui said. The officer asked about her mosque and its imam. He pulled up videos found on her phone, videos of Muslim prayers and Arabic jokes that had circulated among her friends and relatives on social media, and asked her to explain.

Then he asked what she thought of Mr. Trump.

“I said he can do whatever he wants in his country. It’s not my business. I just want to cross the border to go shopping. Why are you asking me this question?” recalled Ms. Alaoui, who wears a hijab.

After they were photographed and fingerprinted, another officer told the women that they would not be allowed into the country because of the videos found on their phones.