Here is another proof for metric spaces. After this proof, it is discussed how this question generalises to the setting of topological spaces.
Let $X$ and $Y$ be metric spaces and $f\colon X\to Y$. Suppose $f(K)$ is compact in $Y$ whenever $K\subseteq X$ is compact. Also suppose $f^{-1}(\{y\})$ is closed for all $y\in Y$. Then $f$ is continuous.
Proof. Since $X$ is a metric space it is sufficient to show that $f$ is sequentially continuous. [See here.] Let $(x_{n})_{n\in\mathbb{N}}$ be a sequence in $X$ that converges to $x\in X$. Suppose for a contradiction that $(f(x_{n}))_{n\in\mathbb{N}}$ does not converge to $f(x)$. Then there is a subsequence $(x_{n_{j}})_{j\in\mathbb{N}}$ of $(x_{n})_{n\in\mathbb{N}}$ and an open set $U$ containing $f(x)$ such that $f(x_{n_{j}})\not\in U$ for all $j\in\mathbb{N}$.
Define $K := \{x_{n_{j}} : j\in\mathbb{N}\}\cup\{x\}$. As $K$ is the image of a convergent sequence with its limit it is compact. [See here.] Then $f(K)$ is compact. Since $(f(x_{n_{j}}))_{j\in\mathbb{N}}$ is a sequence in the compact set $K$ it has a cluster point $y$. [See here.] As $(f(x_{n_{j}}))_{j\in\mathbb{N}}$ is a sequence in the closed set $Y\setminus U$ and cluster points of sequences belong to the closure of the image of the sequence, it follows that $y\in Y\setminus U$. Hence $y\neq f(x)$.
Now the set $f^{-1}(\{y\})$ is closed and does not contain $x$. Consequently, the open set $X\setminus f^{-1}(\{y\})$ contains $x$ and it follows that there is some $j_{0}\in\mathbb{N}$ such that $f(x_{n_{j}}) \in X\setminus f^{-1}(\{y\})$ for all $j\in\mathbb{N}$ with $j\geq j_{0}$. The set $K_{0} := \{x_{n_{j}} : j\geq j_{0}\}\cup\{x\}$ is compact and is contained in $X\setminus f^{-1}(\{y\})$, so it follows that $f(K_{0})$ is a compact set with $y\not\in f(K_{0})$. We also have $y\in \overline{f(K_{0})}$ since $y$ is a cluster point of the sequence $(f(x_{n_{j}}))_{j\in\mathbb{N}}$ that has all its terms eventually in $f(K_{0})$. Now as metric spaces are Hausdorff and compact subsets of Hausdorff spaces are closed, it follows that $\overline{f(K_{0})} = f(K_{0})$. But then $y\in \overline{f(K_{0})}\setminus f(K_{0}) = \emptyset$ which is a contradiction. Hence $(f(x_{n}))_{n\in\mathbb{N}}$ does converge to $f(x)$ . It follows that $f$ is sequentially continuous and therefore, since $X$ is a metric space, continuous.
Note that the above proof in fact shows the following stronger statement.
Let $X$ and $Y$ be topological spaces where $Y$ is Hausdorff. Let $f\colon X\to Y$. Suppose $f(K)$ is compact in $Y$ whenever $K\subseteq X$ is compact. Also suppose $f^{-1}(\{y\})$ is closed for all $y\in Y$. Then $f$ is sequentially continuous.
In the above statement, if $X$ is first countable then the function $f$ is continuous. The assumption that $Y$ is Hausdorff cannot be further weakened. Let $X$, $\tau_{1}$ and $\tau_{2}$ be defined as in the first example from this answer. Then the identity map ${\rm id}\colon (X, \tau_{1}) \to (X, \tau_{2})$ maps compact sets to compact sets. In addition, the preimage of each singleton is closed because $\tau_{1}$ is a $T_{1}$ topology. However, the identity map ${\rm id}\colon (X, \tau_{1}) \to (X, \tau_{2})$ is not sequentially continuous. The details of these claims can be found in this answer.
The following statement is also false in the setting of topological spaces.
Let $X$ and $Y$ be Hausdorff topological spaces and $f\colon X\to Y$. Suppose $f(K)$ is compact in $Y$ whenever $K\subseteq X$ is compact. Also suppose $f^{-1}(\{y\})$ is closed for all $y\in Y$. Then $f$ is continuous.
Define $X := \mathbb{N}\times\mathbb{N} \cup \{(0,0)\}$ where $\mathbb{N} = \{1, 2, \ldots \}$. Let $\tau_{1}$ be the discrete topology on $X$ and let $\tau_{2}$ be the Arens-Fort topology on $X$ such that $(0,0)$ does not have a countable neighbourhood base. Since every compact subset of $(X, \tau_{1})$ is finite we have that ${\rm id}\colon (X, \tau_{1}) \to (X, \tau_{2})$ maps compact sets to compact sets. In addition, because $(X, \tau_{1})$ is $T_{1}$, the preimage of every singleton is closed. However, because the $\tau_{2}$ topology is strictly finer than the $\tau_{1}$ topology, the identity map ${\rm id}\colon (X, \tau_{1}) \to (X, \tau_{2})$ is not continuous.