It's often mis-attributed to Twain, but many believe that it was Lincoln who said it, although proof is somewhat thin. But even Lincoln didn't invent it. The saying has been around in many forms prior to Lincoln, with some versions emerging in Proverbs 17:28 "Even a fool is considered wise if he keeps silent, and discerning when he holds his tongue" and a Chinese Proverb Is it better to keep your mouth shut and seem a fool, or to open your mouth and remove all doubt?
From The Quote Investigator:
The ... Yale Book of Quotations ... investigated the saying and presented the earliest known attribution to Lincoln in Golden Book magazine in November 1931
And also:
When Ken Burns filmed a documentary about Mark Twain in 2001 a companion book was released, and it listed the following version of the quote in a section titled What Twain Didnt Say:
"Better to keep your mouth shut and appear stupid than to open it and remove all doubt."
Ohhhh nooooos.....say it aint so...