The Gemara (Mesechet Shabbat) says that Shavuot can never fall on Shabbat. But it is known that David Hamelech was niftar on Shavuot and Shabbat?
That David died on both Shavuos and Shabbos is not necessarily true. The source that David died on Shavuos can be found in Yerushalmi in Beitzah 11a (2:4) and Chagigah 12a (2:3), but in neither place does it say anything about Shabbos. The source that David died on Shabbos is Bavli Shabbos 30a-b, but they don’t say anything about Shavuos. As all of these are Amoraim, they can argue on one another.
That answer doesn’t work, though, because Rus Rabbah 3:2 says he died on both Shabbos and Shavuos.
However, all is not lost, as there’s no reason to say Shavuos could not have been on Shabbos. David HaMelech lived long before we had a pre-established calendar, rather than being established monthly based on when witnesses came. In our current calendar you are correct that Shavuos can’t fall on Shabbos (OC 429), but in the days before a calendar any day of the year could fall on any day of the week.
For example: Yom Kippur, according to our current calendar, cannot fall out on a Sunday, Tuesday, or Friday (ibid.). However, the Rambam (Hil. Eruvin 8:10) writes that when it’s up to witnesses, Yom Kippur could fall on these days.
In fact, we have another discussion of Shavuos falling on Shabbos. The following is a Mishnah, from Chagigah 2:4 (h/t @DoubleAA):
עֲצֶרֶת שֶׁחָל לִהְיוֹת בְּעֶרֶב שַׁבָּת, בֵּית שַׁמַּאי אוֹמְרִים, יוֹם טְבוֹחַ אַחַר הַשַּׁבָּת. וּבֵית הִלֵּל אוֹמְרִים, אֵין יוֹם טְבוֹחַ אַחַר הַשַּׁבָּת. וּמוֹדִים שֶׁאִם חָל לִהְיוֹת בְּשַׁבָּת, שֶׁיּוֹם טְבוֹחַ אַחַר הַשַּׁבָּת.
When Shavuot occurs on Friday, Beit Shammai says that the day of slaughter [for its burnt offerings] is after Shabbat [i.e., on Sunday]. And Beit Hillel says, that the day after Shabbat is not the day for slaughter [rather it is Friday], but agrees that if Shavuot occurs on Shabbat, the next day [Sunday] is the day of slaughter.
The calendar you're talking about wasn't invented yet. For example, on Rosh haShanah 21a-b there is a discussion about years when both Nissan and Elul are short, whether one has to worry about the possibility of 2 short months in a row, and the like. So the calendar we used wasn't fixed yet.
There is a discussion among rishonim as to whether in the Beis haMiqdash, the month was determined by when witnesses saw the moon, or the month was determined by the courts' computation, and that was when they accepted witnesses to testify about the moon as a formal halachic step in the ritual of declaring the month. (The latter opinion is given by Rabbeinu Chananel on Shemos 12:2.) But even according to the later opinion, gemaros like the one in Rosh haShanah mean that the computation was not the same as the one we use today.
The discussion in the gemara is illustrated with stories of Rabbi Yochanan (180–279 CE), one of his students Rabbi Aivu bar Nagarei and Rabbi Chiyya bar Abba, and Rava (280 – 352 CE). Rava one year fasted a whole "Yom Kippur" only to be told that the actual Yom Kippur was the next day. He fasted 49 hours!
His contemporary, Abayei (d. 338), lived when our computation became standardized, and the calendar would be known in advance. It was his day that the Jews of the Diaspora receieved a letter telling them to continue observing 2 days for each holiday, despite now knowing the date, "minhag avoseikhem beyedeikhem -- the custom of your ancestors is in your hand", now as commemorative rabbinic law. (Beitzah 4b)
So, the calendar dates to the early 4th century CE. Well after King David.
Aside from that, DonielF is right to draw our attention to the reality that amoraim not only argue about halakhah, but they could argue about aggadita too. There is no reason to assume the tradition that says David died on Shabbos is in agreement with the one who says he died on Shavuos.
In general, I am giving the same 2 part answer as he did, just with more detail about the calendar.