We characterize derivations and 2-local derivations from $M_{n}(mathcal{A})$ into $M_{n}(mathcal{M})$, $n ge 2$, where $mathcal{A}$ is a unital algebra over $mathbb{C}$ and $mathcal{M}$ is a unital $mathcal{A}$-bimodule. We show that every derivation $D: M_{n}(mathcal{A}) to M_{n}(mathcal{M})$, $n ge 2,$ is the sum of an inner derivation and a derivation induced by a derivation from $mathcal{A}$ to $mathcal{M}$. We say that $mathcal{A}$ commutes with $mathcal{M}$ if $am=ma$ for every $ainmathcal{A}$ and $minmathcal{M}$. If $mathcal{A}$ commutes with $mathcal{M}$ we prove that every inner 2-local derivation $D: M_{n}(mathcal{A}) to M_{n}(mathcal{M})$, $n ge 2$, is an inner derivation. In addition, if $mathcal{A}$ is commutative and commutes with $mathcal{M}$, then every 2-local derivation $D: M_{n}(mathcal{A}) to M_{n}(mathcal{M})$, $n ge 2$, is a derivation.