I've read that the King of Thailand pardoned 30,000 prisoners on his birthday this year. They are said to be all sick, elderly or close to the end of their sentence. But it seems strange anyway. The idea of the head of state pardoning prisoners seems strange in a modern political system. Why should a single person be given the right to make such arbitrary decisions? And it's not just the King of Thailand. The presidents of my country, Poland, also pardon prisoners on a regular basis. Not in such huge numbers, but they do.
Historically, such pardons were meant to generate goodwill of the governed, and popularize the ruler.
There were two kinds of them:
General pardons. They are simply a good-will gesture and to project a benevolent image.
Political pardons. These were frequently tied to a recently-completed power/political struggle which caused a lot of opponents of the regime to be incarcerated; a wise ruler can use a pardon for low-level opposition supporters to help heal the divisions in society and again generate good will towards his regime from former opponents.
An example of this would be Andrew Johnson's pardons of many former Confederate officials and military personnel after the American Civil War; or George Washington's pardons for Whiskey Rebellion members
In US politics, but I would strongly suspect it was yet another exercise in checks and balances - a way to reverse the most egregious abuses of the law/judicial system, but on a small enough scale that the Executive could not abuse it. The origin of the power (Article II, Section 2 of the United States Constitution states that the President "shall have power to grant reprieves and pardons for offenses against the United States, except in cases of impeachment.") seems to be clearly explained in Alexander Hamilton's Federalist Paper #74:
He is also to be authorized to grant "reprieves and pardons for offenses against the United States, EXCEPT IN CASES OF IMPEACHMENT." Humanity and good policy conspire to dictate, that the benign prerogative of pardoning should be as little as possible fettered or embarrassed. The criminal code of every country partakes so much of necessary severity, that without an easy access to exceptions in favor of unfortunate guilt, justice would wear a countenance too sanguinary and cruel. As the sense of responsibility is always strongest, in proportion as it is undivided, it may be inferred that a single man would be most ready to attend to the force of those motives which might plead for a mitigation of the rigor of the law, and least apt to yield to considerations which were calculated to shelter a fit object of its vengeance. The reflection that the fate of a fellow-creature depended on his sole fiat, would naturally inspire scrupulousness and caution; the dread of being accused of weakness or connivance, would beget equal circumspection, though of a different kind..