I cannot verify that this specific incident is historical, but it is built on solid fact and is "true" in the sense of legend. The religious and social setting is certainly accurate and it may well have happened. More on this below.
Theology of Indulgences
"Buying pieces of heaven" most likely refers to the Roman Catholic Church's tradition of selling Indulgences. This tradition was based on the belief that a faithful Christian whose sins were forgiven still suffers the consequence of sin and:
must be purified either here on earth, or after death in the state
called Purgatory. This purification frees one from what is called the
"temporal punishment" of sin (Catechism of the Catholic Church).
It may reduce the "temporal punishment for sin" after death, in the
state or process of purification called purgatory.
Wikipedia's article is based on Edward N. Peters in A Modern Guide to Indulgences: Rediscovering This Often-Misinterpreted Teaching and the Catechism of the Catholic Church.
"Buying a Piece of Heaven"
Thus, when parishioners paid one hundred coins for an Indulgence, they believed they were "buying a piece of heaven." The parishioners were illiterate peasants who barely had enough for the necessities of life. Martin Luther felt they were manipulated by the church to pay for their own salvation and the salvation of their loved ones; he was outraged. Luther was an educated priest who could read the Bible. He believed salvation was free and that the Church was ripping off the peasants to line its own pockets.
It is not for me to judge the RCC's motivations and theology regarding Indulgences. However, judging from the Catechism of the Catholic Church, the concept of "buying a piece of heaven" is a crude interpretation of the Church's theology, so crude as to be called a misunderstanding. All the same, it was possibly the only way the illiterate peasant farmers could understand the complex theology of Indulgences, familiar as they might have been with buying a piece of land. From there, it would have been but a tiny step to say that Luther "bought hell"; it's merely bringing in Luther's "free salvation" theology in the language of Indulgences.
The Nature of a Folk Legend
As stated, I cannot verify that this story is historically true, though "buying hell" would have spoken to the people's fear of hell and their obligation to buy Indulgences for salvation. Janez Usenik, a writer on Quora, calls this story "an anecdote circulating in some places" and:
fictional propaganda story that meant to demonstrate how protestants
were wiser and better than the catholics
Usenik provides no documentation but I remember my father telling the story when I was growing up in the 1960's; he may have read it somewhere since we were Mennonite and not Lutheran. That is the nature of legends; they are stories that are told and retold down through the generations, sometimes written down, but can never be actually documented and pinned to a specific time and place.
In The Guardian's article Thank Martin Luther for exposing the church’s great salvation sell-off, Giles Fraser may also be referring to this story, though he does not say it in so many words. He mentions a "letter of protest."
Martin Luther was an unknown monk in an ecclesiastical backwater
before he wrote Albert a letter of protest. People in the pews were
being ripped off. And worse still, the church was twisting authentic
Christian theology for financial gain. Salvation wasn’t for the church
to buy and sell. First, it was free. And second, it was entirely the
gift of God. The letter was sent on 31 October 1517, and, as legend
has it, nailed to the door of All Saints, Wittenberg.
It is unclear whether "the letter of protest" was a cover letter to accompany the 95 Theses, or whether Fraser called the 95 Theses themselves a letter of protest. The 95 Theses are well-documented and historical, according to the Waterloo Lutheran Seminary where I studied in the early 2000's.
The Ninety-five Theses or Disputation on the Power of Indulgences
is a list of propositions for an academic disputation written in 1517
by Martin Luther, professor of moral theology at the University of
Wittenberg, Germany. They advance Luther's positions against what he
saw as the abuse of the practice of clergy selling plenary
indulgences, which were certificates believed to reduce the temporal
punishment in purgatory for sins committed by the purchasers or their
loved ones. (Emphasis added.)
This ties together the idea of buying Indulgences for money, and that a certificate represents (is) the Indulgence. In other words, the story in the Question says the priest gave Martin Luther a certificate when he paid for Hell. My point: It was normal for the priest to give the purchaser of an Indulgence a certificate upon purchase. We might call it a proof of purchase.
For more background on the religious situation of the time, go to this page and scroll down to "The 95 Theses: Martin Luther Challenges the Church."