The 22-year-old Khachanov was in devastating form at the start as he dropped only two points in his first four service games. After bagging the first set tiebreak, he then unexpectedly lost his way.
The 53rd-ranked Berrettini fired down nine aces en route to booking a place in the quarter-finals.
"It was a very difficult first set, I didn't play so well," said Berrettini.
"But then I improved my performance in the second set and I think I played very well in the third."
Results from the ATP Sofia Open on Wednesday (x denotes seeding):
Second round
Matteo Berrettini (ITA) bt Karen Khachanov (RUS x1) 6-7 (6/8), 6-3, 6-4
Roberto Bautista Agut (ESP x4) bt Matthew Ebden (AUS) 3-6, 6-3, 6-2
First round
Fernando Verdasco (ESP x6) bt Alexandar Lazarov (BUL) 6-2, 6-1
Marton Fucsovics (HUN) bt Andreas Seppi (ITA x8) 6-2, 7-6 (7/2)
Martin Klizan (SVK) bt Mirza Basic (BIH) 6-0, 3-6, 6-2
Mikhail Kukushkin (KAZ) bt Laslo Djere (SRB) 7-6 (8/6), 6-4