What an excellent question!
Aaron had four sons: two died an early death with no survivors, and the other two sons survived:-- Eleazar and Ithamar. Thus the Levites who served as priests at the time of David are all descended from one of these two priestly lines.
In the Hebrew Bible, when we see the Ahimelech(father)/Abiathar(son) team, these two are the direct descendants of Ahitub (thus the priestly line of Eleazar).
When we see the Abiathar(father)/Ahimelech(son) team, these two are the direct descendants of Ichabod (thus the priestly line of Ithamar). Thus the guy who gave to David and his men the sacred consecrated bread to eat was Ahimelech (the son), who served at the time when his father Abiathar was the priest. Please see 1 Chronicles 18:16 in addition to 1 Sam 21:1-9 compared with Mark 2:26.
Ichabod and Ahitub happened to be brothers (1 Samuel 14:3), but they had different fathers. That is, according to this verse the father of Ichabod was Phineas, and of course the father of Ahitub was Amariah (according to 1 Chronicles 6:7 and 1 Chronicles 6:11).
So what is confusing is that there are two sets of people in the SAME tribe of Levi (priests from both the line of Eleazar and Ithamar) with the SAME names occurring in the SAME passages with limited clarification by the authors of the texts of exactly who is who.
(Very necessary but quick sidebar: The father of Phineas was Eli, who received the divine judgment from God that all the priestly descendants of Eli would die young according to 1 Sam 2:31-33 -- that is, no one in the priestly line of Eli would live to grow old. For example, 1 Chronicles 24:1-4 indicates that there were many less men available in the priestly line of Ithamar compared to the priestly line of Eleazar.)
We know that the Abiathar who was fired as a priest by King Solomon is the Abiathar who is the descendant of Eli (and therefore from the priestly line of Ithamar per 1 Kings 2:26-27). If Solomon was aware of the divine imprecation on the priestly line of Eli, then there was no need for him to kill him, but rather to dismiss him since the pronouncement of God's judgment would end his life early anyway. The “good” Abiathar then moves in and takes the “bad” Abiathar's job in 1 Kings 4:4. In other words, the “bad” Abiathar was not one of the eighty-five men who wore the linen ephod and who were killed by Saul in 1 Samuel 22:18. The other Abiathar (the “good” Abiathar), who occurs in the same time and space, and who also escaped this massacre, goes and helps David. (This goodwill eventually postured him to replace the “bad” Abiathar.) This “good” Abiathar is the son of Ahimelech, who is the son of Ahitub, who is the son of Amariah (which takes us back to the priestly line through Eleazar). Again, the “good” Abiathar has absolutely no connection whatsoever with the priestly line of Eli (descendants of the priestly line of Ithamar) as was the case with the “bad” Abiathar.
In summary, the confusion is understandable when one sees in the genealogy of Levi, for example, in 1 Chronicles 6 the names of the priests are repeated several times in the same family tree (e.g., the names of Elkanah, Ahitub, and Amariah occur several times among the Levites). It is no surprise then that two “Abiathars” and two “Ahimelechs” also create confusion when they happen to exist and live in the same time and space of David and Solomon. In fact it is not uncommon even in our modern era to find the same relatives, be it cousins, aunts, and uncles, and even fathers & sons, who happen to “share” the same christened names (not to mention middle names) amongst themselves.