OTTAWA — A Somali man suspected of carrying out what was called a terrorist attack in Alberta last weekend came to Canada and was declared a refugee after being ordered expelled from the United States several years ago, officials said on Wednesday.

The man, Abdulahi Sharif, 30, is accused of striking a police officer with a car and stabbing him outside a football stadium in Edmonton, Alberta, on Sunday and later using a rental truck to hit four people elsewhere in the city. He faces five counts of attempted murder, five counts of dangerous operation of a motor vehicle, one count of criminal flight causing bodily harm and one count of possession of a weapon.

The Royal Canadian Mounted Police have said that Mr. Sharif showed signs of extremism two years ago, leading to an investigation, although no charges were brought against him.

The United States Immigration and Customs Enforcement service said that it detained Mr. Sharif in July 2011 in California after he was found near the Mexican border without documentation, and that a judge in September of that year ordered him returned to Somalia. He was released from a detention center two months later, however, “due to a lack of likelihood of his removal in the reasonably foreseeable future,” the agency said.