Before I attempt to provide an explanation for why the events of 2 Samuel 24 are located in the final chapter, let me provide some additional observations and evidence that support a dating of such in the early years of David:
1. Appendix to 2 Samuel
This point has already been mentioned in other answer, but I would still like to explore it a little further. It is quite evident that the final 4 chapters of 2 Sam (21-24) are not chronological, and belong to an appendix possibly added later to the book. The chapter 23 exemplifies this even better, citing an event that may have happened before David even became king, or, at the latest, shortly after Saul's death and his subsequent coronation:
2 Samuel 22:1 (KJV)
And David spake unto the LORD the words of this song in the day that the LORD had delivered him out of the hand of all his enemies, and out of the hand of Saul:
A similar case should be applied to chapter 21, whose break in narrative continuity with chapter 20 is notable. 2 Sam 21 talks about a three-year famine that hit the country, as a result of a massacre carried out by Saul against the Gibeonites. To appease the situation, David then hands over seven of Saul's descendants to be killed, and the situation is then resolved.
Leaving aside the moral problems of this chapter, it seems illogical to me that God would take so long to send the famine, and likewise that the crime would only be punished decades after the fact, making the punishment even more unjustifiable. Also, it is possible that Shimei's speech in 2 Sam 16:7-8 is referring directly to this event, placing it before Absalom's revolt, that is, in the first half of David's reign, probably shortly after he becomes aware about Mephibosheth in the chapter 9.
The only chapter that appears to be chronological here is 23, the rest being probably results of posterious insertions, placed at the end whether intentionally or not.
2. The Case of the Gibeonites
As I have already mentioned, the case of the Gibeonites, reported in chapter 21, must belong to the early years of David. However, it is also possible that this census event occurred shortly after this last episode. The reason for this theory lies in the discrepancy between 2 Sam 24 and its parallel account in 1 Chr 21:
2 Samuel 24:13 (KJV)
So Gad came to David, and told him, and said unto him, Shall seven years of famine come unto thee in thy land? or wilt thou flee three months before thine enemies, while they pursue thee? or that there be three days' pestilence in thy land? now advise, and see what answer I shall return to him that sent me.
1 Chronicles 21:11-12 (KJV)
So Gad came to David, and said unto him, Thus saith the LORD, Choose thee either three years' famine; or three months to be destroyed before thy foes, while that the sword of thine enemies overtaketh thee; or else three days the sword of the LORD, even the pestilence, in the land, and the angel of the LORD destroying throughout all the coasts of Israel. Now therefore advise thyself what word I shall bring again to him that sent me.
One mentions an offer of seven years of famine, while the other mentions three years, as you can see. Among the theories to resolve the issue, in general, those that do not resort to a scribal error in a of the two passages, take both as true by making 2 Sam 24 occur immediately after chapter 21. Thus, what Gad actually offered David was three additional years of famine, which, added to the three years already resulting from due to the case of the Gibeonites, and the year of the census, would total a total of seven years.
If this theory is true, then the census took place soon after the death of Saul's descendants. And if the second, as we have discussed before, must have occurred in David's early years, the first should also do so.
3. Presence of Gad the Seer
Already mentioned by the OP, but I want to offer some additional points here.
During David's life we can basically see the activity of three prophets: Samuel, Gad and Nathan. Samuel appears only in the episode of David's anointing, and after that he has no further contact with him. Gad appears to be his successor, first appearing in 1 Sam 22, probably at the time Samuel would have been too old to be active, since his death is recorded a few chapters later. As for Nathan, his first appearance is in 2 Sam 7, in the episode of David's attempted construction of the Temple. After this, he continues to be occasionally mentioned until the beginning of Solomon's reign.
If Gad apparently succeeded Samuel, it is possible that Nathan also succeeded Gad, who would then have served in the early period of David's reign, before chapter 7. If the census event had been in David's final years, we would certainly see the figure of Nathan being sent to David, not Gad.
4. The Location of the Tarbenacle and the Construction of the Temple
This I believe presents the strongest evidence for the early dating of the census. We should pay attention to the final verses of the parallel account in 1 Chronicles:
1 Chronicles 21:28-30 (KJV)
At that time when David saw that the LORD had answered him in the threshingfloor of Ornan the Jebusite, then he sacrificed there. For the tabernacle of the LORD, which Moses made in the wilderness, and the altar of the burnt offering, were at that season in the high place at Gibeon. But David could not go before it to enquire of God: for he was afraid because of the sword of the angel of the LORD.
1 Chronicles 22:1 (ESV)
Then David said, “Here shall be the house of the Lord God and here the altar of burnt offering for Israel.”
For the first point highlighted, compare with 1 Chr 16:
1 Chronicles 16:39 (ESV)
And he [David] left Zadok the priest and his brothers the priests before the tabernacle of the Lord in the high place that was at Gibeon
1 Chr 16 narrates the transfer of the Ark of the Covenant to Jerusalem, and this passage seems to imply that during the event the tabernacle was also moved, to Gibeon. All previous references to the tabernacle placed it in Shiloh. It is possible that the Tabernacle had also been moved there in an earlier episode (such as after the massacre of the Priests at Nob, in 1 Samuel 22), but I still believe that the Ark must have been moved soon after the conquest of Jerusalem, there would be no reason to wait any longer.
The second text, 1 Chr 22:1, records that, after the events of the previous chapter, David decided that the future house of the God, that is, the Temple, would be built there. The following verses record David gathering materials for this building, and although this is linked to Solomon further ahead, I believe it must actually belong to the first attempt at building in 2 Sam 7 and 1 Chr 17:
2 Samuel 7:2-3 (KJV)
that the king said unto Nathan the prophet, See now, I dwell in an house of cedar, but the ark of God dwelleth within curtains. And Nathan said to the king, Go, do all that is in thine heart; for the LORD is with thee.
Later, God orders Nathan to return and tell David that he will not be to build the Temple, but that a David's "descendant" would do so. But probably, either before Nathan returned, or even after his return, it is possible that David began the preparations for the works, later reserving them for the future reign of Salomon.
Since Nathan refers to a "descendant" without explicitly naming Solomon, this seems to imply that he would not yet be born, and the very order of the chapters here indicates that this event it was also at the beginning of David's reign, shortly after the transfer of the capital to Jerusalem.
That is, adding up all this evidence, the census must have occurred between the period of chapters 6 and 7 of 2 Samuel (16 and 17 in 1 Chronicles), shortly after the transfer of the capital and the Ark to Jerusalem, but before the first preparations for the initial attempt to build the Temple and the birth of Solomon. This places us between the 7th and 20th year of David's reign, quite a distance from the end of his reign.
It is also much easier to explain why David took a census at the beginning of his reign, now over all Israel, than at the end. Remember that censuses at this time were mainly to account for the nation's military might, which would have been of interest to David when newly crowned, but perhaps not so much on the brink of death.
But if the census took place early in David's reign, why is it referred to at the end of 2 Samuel?
And finally we come to the central question of the OP, and here it is a little more difficult to build a theory, but not impossible. First of all, I think it would be very unlikely that an entire chapter would have been moved from the middle to the end of the book, either arbitrarily or accidentally, so yes, 2 Samuel 24 has always been where it is. But nothing prevents the chapter from originally not existing in the book.
Keep in mind, again, that the final chapters of 2 Samuel are an appendix, and that the entire book of Samuel was most likely the work of multiple authors (see 1 Chr 29:29). Perhaps the final editor did not know exactly where to position these final chapters chronologically, and to avoid problems, he placed everything in the final appendix. Or perhaps these accounts were not present in the book and were later added by a fourth author or even by the editor, who, in order to avoid discontinuing the already established narrative, allocated everything once again to the final appendix.
Another reason may be for the sake of narrative continuity, since the account of 2 Sam 7 seems to fit well after the account of 2 Sam 6, they are similar thematic units. It would be necessary the break this to insert the account of a great sin of David. Why not put it at the end, after other great sins of David?
One more possibility could still have to do with the connection of the Temple with Solomon's time, which is more evident in 1 Chr 22. The chapter links the event, which precedes the building of the Temple, to the future building of the Temple by Solomon. Thus, we do not break the thematic unity of the beginning of the book, as well as we also link it to the thematic unity of the construction of the Temple and the reign of Solomon.
There may be some combination of two or more of these theories, but whatever the reason for why 2 Samuel 24 is where it is, it definitely does not belong at the end of David's reign.