I would offer three answers, which I believe may be true simultaneously.
While scribes were extremely careful for pesukim in Tanach, so as not to invalidate the kosher status of the sefer, they were not so careful when quoting pesukim when they occurred within the Gemara.
Add to that that sometimes earlier manuscripts will shorten words or phrases with apostrophes -- e.g., they might write ושוטר or ושוטרי followed by an apostrophe, and a later scribe could expand it into what he believed was the full quote, working from memory because he did not have the quote before him.
In the two cases given in the question, this sort of accidental expansion seems likely. שני could be expanded accidentally into שנים. And in Yehoshua the words are וכל ישראל וזקיניו ושטרים ושפטיו, when shortened and then expanded, would have ושטרים naturally expanded to the יו ending, based on context. Lectio difficilior and all that.
I heard in Rav Schachter's shiur, I forget citing whom, that Chazal deliberately messed up the quotations. After all, one should not be citing a pasuk, from the Written Law, orally. Therefore, they changed the pesukim in ways that would not damage the derasha.
This however does not work for the many instances in which the derasha is made based on a feature of the changed text. For example, Chazal darshen the chaser / malei status of Totafot against what is our current masoretic spelling.
This seems to indicate that, at least in those instances, and possibly in others, they are working from a different, parallel masorah.
(Vetus Testamentum for the two examples in the question, has the same as our own Masoretic text, but cites other Hebrew texts, meaning from Jews rather than Samaritans, that in fact have these variants.)
I have myself investigated many of these instances of changed wording and discovered that Chazal's derasha accords with an ancient text we can actually find, namely in the Samaritan Pentateuch. That means that whoever made the derasha had a different text in his sefer Torah, from which he darshened.
For example, I posted about a Midrash Rabba that assumes the Samaritan text, and causes confusion among the commentators of Midrash Rabba. And in another, I show that rather than following the Gra's emendation of a quote of a pasuk in a gemara, we can see that they are actually darshening the Samaritan text. And in another, I show how the famous question on the Haggadah about how the Chacham can say אתכם and not be wicked is based on our Masoretic text, but in old Haggadahs and in the Mechilta, the Chacham repeats the pasuk but says Otanu, in line with e.g. the Septuagint: