here's a good explanation (it's been this way for a century or more):
A pitcher who is relieved mid-inning may be charged with earned runs equal to the number of batters who reached base while he was pitching, even if the specific batters he faced do not score. The batters he put on base may be erased by fielder's choice plays after he has been relieved by another pitcher, but if earned runs are scored in the inning the original pitcher is liable for as many earned runs as the number of batters he put on base. Example:
On April 15, 2017, Detroit's Justin Verlander allowed the first two Cleveland batters in the 5th inning to reach base on base hits; Verlander was then relieved by Shane Greene. Greene walked the next batter to load the bases. The next batter hit a grounder and Miguel Cabrera threw home to force out the runner on third in a fielder's choice, so the bases remained loaded with one out. Greene struck out the next batter for the second out. Carlos Santana then hit a single that scored the runners from second and third (only one of whom was put on base by Verlander), and the runner from first was thrown out at the plate to end the inning. Since Verlander allowed two batters to reach base he was charged with two earned runs, even though only one of the two specific batters he faced actually scored.
When a pitching change occurs, the new pitcher is said to "inherit" any runners that are on base at the time, and if they later score, those runs are charged (earned or unearned) to the prior pitcher. Most box scores now list inherited runners, and the number that scored, as a statistic for the relief pitcher.