Borrowed from Old Tupi ananá. The final ⟨s⟩ is unetymological as no word in Old Tupi could end in /s/ and possibly results from a back-formation from ananazeiro (“pineapple plant”) — compare araçás. First attested in the 16th century.
Practice speaking and memorizing "ananás" and many other words and sentences in Portuguese.