Yes, a masked prisoner certainly was kept incarcerated in a series of French prisons between 1661 and his death in 1703. The mask was not made of iron, though - consensus suggests it was probably made of whalebone faced with velvet, and that it may only have been worn when he was likely to be seen in public, for example during a transfer between prisons. And while the man's existence was known at the time – a contemporary gazette mentions his transfer from one prison to another in 1687 – the case owes much of its current fame to the work of Victor Hugo, who wrote an influential novel about the case in 1847-50 as part of the wildly popular series of books he authored about the Three Musketeers.
The real puzzle, of course, concerns the masked man's identity. A large amount of research has been done in an attempt to solve the problem, and it has turned up a small quantity of contemporary prison records that do little but heap up more mystery. For example, the memoirs of an officer of the Bastille prison notes that a prisoner arrived in Paris who was " always masked and whose name is never pronounced.” Records of his burial suggest he was in his 50s when he died - which if true would mean that he was aged in his late teens when incarcerated, and thus hint at a motive that had more to do with who he was than what he might have done. They add that all his belongings were burned after his death. There are plenty of other details, some more plausible than others but all of them essentially folklore, to suggest that someone very senior indeed in the French state, or perhaps some state organisation, wanted to make sure that the man's identity remained a secret. For instance, there are several accounts alleging that the walls of his cell were scraped and whitewashed after he died to ensure no messages were left on them. (Some of this work was in vain; a couple of years ago, French archivists exhumed two inventories of the prisoner's possessions from the endless word-mine of the National Archives.) And we know that he was moved from prison to prison in order to ensure he remained under the care of Bénigne de Saint-Mars, one of the most senior of French prison governors - though not why Saint-Mars, of all those in the service, was selected for the job.
Most of the speculation that has surrounded the Man in the Iron Mask suggests he was incarcerated on the order of Louis XIV. This is not implausible; French monarchs could make use of a device called a lettre de cachet to issue orders that had to be carried out without any recourse to legal process or appeal. But there's no firm evidence that the man was specifically Louis's prisoner. A large part of the reason that we think he was has a direct bearing on your question, because most of the earliest accounts of the Iron Mask case were the product of anti-French propaganda. For example, the Dutch used the case to try to undermine Louis's legitimacy during the Nine Years War (1688-97), alleging that the prisoner was the former lover of the Queen Mother, and in fact the Sun King's real father. Other pamphlets were published to suggest the prisoner was the king's bastard half brother, or a lover of Louis's wife. Voltaire - who picked up rumours of the prisoner's existence when he was locked up in the Bastille briefly in 1717 – was a proponent of the theory that the prisoner was the king's brother, and it is also to Voltaire that we owe the idea that his mask was made of iron. Hugo's novel elaborates on this suggestion, making the prisoner Louis's twin, and hence a direct threat to his position as king.
Modern historians reject these ideas and several have sought to identify a candidate who meets the most obvious criteria for the Mask – someone whose existence was an embarrassment to the state and who was likely to be recognised if seen. It's fair to say that no-one of the necessary eminence who fits the known dates of the case has yet been identified. If we set aside the idea that the man was as young as in his late 50s when he died in 1703, and hence might have been imprisoned for what he did rather than for who he was, then a reasonable guess would be that the prisoner was involved in some potentially highly embarrassing court scandal, along the lines of the infamous Affair of the Poisons, which had the potential to gravely compromise the Crown. The longest-serving female prisoner in French history was Magdelaine (or Madelaine) Chapelain, a Parisienne fortune-teller caught up in the Poisons case, who was incarcerated for 44 years.
The Man in the Iron Mask spent 42 years in prison, but you might like to know that this doesn't come close to winning any awards for duration, even in France; the current record, which I can't imagine is one anyone would want to break, is just shy of 71 years. I wrote about the longest prison sentences ever served in detail here (mentioning the Iron Mask case in passing).
Sources
Rupert Gould, Oddities (1928)
Paul Sonnino, The Search for the Man in the Iron Mask (2016)