In the case of the Ramayana, the exile was 14 years so that Bharata would have enough time to grow close to the people, since previously Rama was beloved by the people and Bharata had been living away from Ayodhya with his maternal uncle; here is what Manthara tells Kaikeyii in the Ayodhya Kanda of the Ramayana:
Hence, you have to stop, by force, the arrangements being made for Rama's coronation. You ask your husband as two boons, anointing Bharata for princely kingdom and sending Rama to exile for fourteen years. If you send Rama to forest for fourteen years, your son Bharata will get intimate association in the hearts of the people and will get stabilised in kingdom.
Now as far as the Mahabharata goes, Shakuni provides no justification for why he chooses this particular stakes 12 years plus 1 year incognito for the game of dice; this is all he says about the stakes in the Sabha Parva of the Mahabharata:
But, O bull of the Bharata race, listen to me, there is a stake of great value. Either defeated by ye at dice, dressed in deer skins we shall enter the great forest and live there for twelve years passing the whole of the thirteenth year in some inhabited region, unrecognised, and if recognised return to an exile of another twelve years; or vanquished by us, dressed in deer skins ye shall, with Krishna, live for twelve years in the woods passing the whole of the thirteenth year unrecognised, in some inhabited region. If recognised, an exile of another twelve years is to be the consequence. On the expiry of the thirteenth year, each is to have his kingdom surrendered by the other. O Yudhishthira, with this resolution, play with us, O Bharata, casting the dice.
So the winning side would either enjoy the kingdom for 13 or 25 years depending on whether the losers are recognized while in their year of hiding. Like I said Shakuni doesn't say why he chose these numbers, but we can surmise that just as in the case of the Ramayana, they were chosen so that the winning side would have quite a long time to become close to the people.