The idea of the plot was suggested to Trollope by his brother Thomas.
While her brother is in prison, Mary gives birth to a girl. A former suitor offers to marry her and emigrate to start a new life, but refuses to take the baby. Doctor Thorne persuades her to accept the generous offer, promising to raise his niece. He names her Mary Thorne, but not wishing to have her illegitimacy made public nor have her associate with the uncouth Roger Scatcherd, he keeps her birth secret. He tells Scatcherd that the baby had died.
After his release, Scatcherd rises quickly in the world. In time, his skills make him extremely rich. When he completes a seemingly-impossible important project on time, he is created a baronet for his efforts. Throughout his career, he entrusts his financial affairs to Doctor Thorne. When Thorne becomes the family doctor to the Greshams, he persuades Scatcherd to loan ever growing sums to the head of the family, the local squire. Eventually, much of the Gresham estate is put up as collateral.
Meanwhile, Mary grows up with the Gresham children and becomes a great favorite with the whole family. As a result, Thorne feels obliged to tell his friend the squire her secret.
Mary falls in love with Frank Gresham, the son and heir of the squire of Greshamsbury and nephew of the Earl and Countess De Courcy, and he with her. However, his parents desperately need him to marry wealth, in order to rescue them from the financial distress resulting from the squire's repeated expensive and fruitless political aspirations.
His snobbish mother and aunt wish him to marry an eccentric, if kind-hearted older heiress, Martha Dunstable. He reluctantly visits her at Courcy Castle and they become friends. He foolishly and playfully proposes. She demurs, knowing that he does not love her, and he tells her about his love for Mary.
Sir Roger is a drunkard, and Doctor Thorne tries in vain to get him to curtail his drinking. In his will, he stipulates that bulk of his estate go to his odious, dissolute only son Louis, but leaves Doctor Thorne in control of the inheritance until the heir reaches the age of twenty-five. Should Louis die before then, Scatcherd stipulates that the estate go to his sister Mary's eldest child. Thorne is forced to divulge Mary's history, but Scatcherd leaves the will unchanged.
The patient eventually dies of his excesses, and Sir Louis inherits his vast wealth. Conveniently, the son proves to be just as much an alcoholic as the father and his weaker constitution causes him to quickly suffer the same fate. After consulting with many lawyers, Doctor Thorne confirms that his Mary is the heiress, much richer even than Miss Dunstable.
Unaware of these proceedings, the more-resolute Frank finally convinces his doting father to consent to his marriage to Mary. When all is revealed, everyone is elated, even Frank's mother and Countess De Courcy.