Why are Ahimelech and Abiathar reversed in 2 Sam. 8:17? In 1 Samuel 22:20-23, we see that Ahimelech was killed by the order of Saul and his son Abiathar escaped:


  20 But one son of Ahimelech the son of Ahitub, named Abiathar, escaped
  and fled after David. 21 Abiathar told David that Saul had killed the
  priests of the LORD. 22 Then David said to Abiathar, “I knew on that
  day, when Doeg the Edomite was there, that he would surely tell Saul.
  I have brought about the death of every person in your father’s
  household. 23 “Stay with me; do not be afraid, for he who seeks my
  life seeks your life, for you are safe with me.”


However, in 2 Samuel 8:17, Ahimelech is referenced as one of David's priests, and the son of Abiathar:


  17 and Zadok the son of Ahitub and Ahimelech the son of Abiathar were priests, and Seraiah was secretary, 


Obviously both can't be true, but I can't seem to find any compelling information that would explain this reversal of names.
I have a list of alleged Biblical contradictions that contains this question.


We must first attempt to establish a reference point in time for these verses. 


1 Samuel 22:20-23 occurred before David ascended to the throne.  King Saul was then in pursuit of David, and had recently ordered the deaths of 85 priests, after accusing them of treason [1 Samuel 22:17, 18].  Abiathar, son of Ahimelech, was one who had escaped death.  
2 Samuel 8:17 occurred at least 7 1/2 years into David's forty-year reign [2 Samuel 5:4].  


2 Samuel 5:5:  David reigned over Judah in Hebron 7 1/2 years; then 33 years over all Israel in Jerusalem.  2 Samuel 8 was during his [early?] reign in Jerusalem.  


Next, we must attempt to identify these men.  


"Ahimelech" was a name given to at least two men that David knew:  


The first was identified as a priest in 1 Samuel 22:20-23. 
The second was identified in 1 Samuel 26:6 as "Ahimelech the Hittite."  
Though possessing the same name, these are clearly not the same person.  

What, then, of the third Ahimelech, in 2 Samuel 8:17?  


We know he was a priest; apparently, he was much more.  


Zadok was not only a priest in David's reign; he was the High Priest--the lineage can be verified in Ezra 7:1-5.  
This Ahimelech was apparently a co-High Priest; he is paired with Zadok several other times in Scripture:  2 Samuel 15:24, 29, 35; 17:15; 19:11; 20:25.  
He must have died early in Solomon's reign; in 1 Kings 4:4, we read of "Zadok and Abiathar" as priests.      



I conclude, though "Ahimelech" and "Abiathar" are named in both passages, they are not the same two men in both passages.  Therefore, there is no contradiction.