WASHINGTON—The House passed a resolution condemning as racist President Trump’s tweets calling for four U.S. congresswomen to “go back” to unspecified countries as Mr. Trump remained defiant and lashed out again at the Democratic lawmakers.

The House vote was 240 to 187, largely along party lines, with four Republicans joining 235 Democrats and one independent, Justin Amash of Michigan, in voting in favor of the resolution.

The vote came at the end of a chaotic afternoon on the House floor, in which Speaker Nancy Pelosi’s characterization of Mr. Trump’s tweets as racist was ruled out of order by the chair, but were then kept on the record in a party-line vote, underscoring the divisive and unusual nature of Mr. Trump’s comments.

The largely symbolic resolution that passed Tuesday evening resolves that the House “strongly condemns President Donald Trump’s racist comments that have legitimized and increased fear and hatred of new Americans and people of color.” It now moves to the GOP-controlled Senate, which isn’t expected to take it up.

The controversy started Sunday, when Mr. Trump tweeted that the congresswomen, who are all minorities, came from corrupt and broken countries and should “go back” to fix what he called the “totally broken and crime infested places from which they came.” As lawmakers, all are U.S. citizens, and all but one were born in the U.S.