And how is this tied to Mary, and her Heart exactly?
First, here is a bit on the history of the Immaculate Heart of Mary devotion (From New Advent)
It is only in the twelfth, or towards the end of the eleventh century, that slight indications of a regular devotion are perceived in a sermon by St. Bernard (De duodecim stellis), from which an extract has been taken by the Church and used in the Offices of the Compassion and of the Seven Dolours. Stronger evidences are discernible in the pious meditations on the Ave Maria and the Salve Regina, usually attributed either to St. Anselm of Lucca (d. 1080) or St. Bernard; and also in the large book "De laudibus B. Mariae Virginis" (Douai, 1625) by Richard de Saint-Laurent. Penitentiary of Rouen in the thirteenth century. In St. Mechtilde (d. 1298) and St. Gertrude (d. 1302) the devotion had two earnest adherents. A little earlier it had been included by St. Thomas Becket in the devotion to the joys and sorrows of Mary, by Blessed Hermann (d.1245), one of the first spiritual children of St. Dominic, in his other devotions to Mary, and somewhat later it appeared in St. Bridget's "Book of Revelations". Tauler (d. 1361) beholds in Mary the model of a mystical, just as St. Ambrose perceived in her the model of a virginal soul. St. Bernardine of Siena (d.1444) was more absorbed in the contemplation of the virginal heart, and it is from him that the Church has borrowed the lessons of the Second Nocturn for the feast of the Heart of Mary. St. Francis de Sales speaks of the perfections of this heart, the model of love for God, and dedicated to it his "Theotimus".
During this same period one finds occasional mention of devotional practices to the Heart of Mary, e.g. in the "Antidotarium" of Nicolas du Saussay (d.1488), in Julius II, and in the "Pharetra" of Lanspergius. In the second half of the sixteenth century and the first half of the seventeenth, ascetic authors dwelt upon this devotion at greater length. It was, however, reserved to St. Jean Eudes (d. 1681) to propagate the devotion, to make it public, and to have a feast celebrated in honor of the Heart of Mary, first at Autun in 1648 and afterwards in a number of French dioceses. He established several religious societies interested in upholding and promoting the devotion, of which his large book on the Coeur Admirable (Admirable Heart), published in 1681, resembles a summary. Père Eudes' efforts to secure the approval of an Office and feast failed at Rome, but, notwithstanding, this disappointment, the devotion to the Heart of Mary progressed. In 1699 Father Pinamonti (d. 1703) published in Italian his beautiful little work on the Holy Heart of Mary, and in 1725 Père de Gallifet combined the cause of the Heart of Mary with that of the Heart of Jesus in order to obtain Rome's approbation of the two devotions and the institution of the two feasts. In 1729 his project was defeated, and in 1765 the two causes were separated, to assure the success of the principal one.
In 1799 Pius VI, then in captivity at Florence, granted the Bishop of Palermo the feast of the Most Pure Heart of Mary for some of the churches in his diocese. In 1805 Pius VII made a new concession, thanks to which the feast was soon widely observed. Such was the existing condition when a twofold movement, started in Paris, gave fresh impetus to the devotion. The two factors of this movement were first of all the revelation of the "miraculous medal" in 1830 and all the prodigies that followed, and then the establishment at Notre-Dame-des-Victoires of the Archconfraternity of the Immaculate Heart of Mary, Refuge of Sinners, which spread rapidly throughout the world and was the source of numberless graces. On 21 July, 1855, the Congregation of Rites finally approved the Office and Mass of the Most Pure Heart of Mary without, however, imposing them upon the Universal Church.
Now there are at least three feasts of the Heart of Mary, all with different Offices:
that of Rome, observed in many places on the Sunday after the Octave of the Assumption and in others on the third Sunday after Pentecost or in the beginning of July;
that of Pere Eudes celebrated among the Eudists and in a number of communities on 8 February; and
that of Notre-Dame-des-Victoires, solemnized a little before Lent.
However, no feast has as yet been granted to the entire Church.
Now as to its connection to Jesus and his heart. The prophet in the temple told Mary, "A sword will pierce your heart also..." (Gospel of Luke)
Just as Mary is always honored when she is seen in the light of her Divine Son, so too Mary's heart, that was so united to His passion that the prophet could speak the words above.