The short answer
Quote Investigator found no evidence that Twain, who died in 1910, said or wrote it. The earliest strong match it located is in an essay by the psychoanalyst Theodor Reik, “The Unreachables”, published in 1965: “It has been said that history repeats itself. This is perhaps not quite correct; it merely rhymes.” The first credit to Twain it found is from 1970.
What Twain did write about history
Twain did use the phrase “History never repeats itself”, in The Gilded Age, the novel he wrote with Charles Dudley Warner. In chapter XLVII a mock newspaper editorial begins: “History never repeats itself, but the Kaleidoscopic combinations of the pictured present often seem to be constructed out of the broken fragments of antique legends.” The idea is close; the rhyme is not there.
An older rhyme
The image of history rhyming is older than Reik. Quote Investigator cites an 1845 article in The Christian Remembrancer: “history repeats her tale unconsciously, and goes off into a mystic rhyme; ages are prototypes of other ages.” The compact, witty version seems to have arrived only in the 1960s, half a century after Twain's death, and his name made it travel.