I'll use $\overline{\mathbb{R}}$ to denote the extended real numbers, i.e. $[-\infty,\infty]$. But the argument works just as well with plain old $\mathbb{R}$, or even any measurable subset of $\mathbb{R}$ containing $0$, or even any measurable space $Y$ (with some $y\in Y$ replacing $0$).
Let $X$ be a measurable space with $\sigma$-algebra $\mathcal{M}$, and let $U\subseteq X$ be a measurable set (i.e. $U\in\mathcal{M}$), made into a measurable space with the $\sigma$-algebra $\mathcal{N}=\{A\cap U\mid A\in\mathcal{M}\}$. Note that $\mathcal{N}\subseteq\mathcal{M}$, because the intersection of any two sets in $\mathcal{M}$ is in $\mathcal{M}$.
Let $f:U\to\overline{\mathbb{R}}$ be a measurable function, and define $F:X\to\overline{\mathbb{R}}$ by $F(x)=\begin{cases} f(x)\quad\,\text{ if }x\in U\\0\quad\quad\quad\text{ if }x\notin U.\end{cases}$ $F$ is measurable if and only if, for any measurable subset $S\subseteq\overline{\mathbb{R}}$, the set $F^{-1}(S)$ is a measurable subset of $X$, i.e. $F^{-1}(S)\in\mathcal{M}$.
If $0\notin S$, then $F^{-1}(S)=f^{-1}(S)$. Because $f$ is measurable, we have that $f^{-1}(S)\in\mathcal{N}$, and because $\mathcal{N}\subseteq\mathcal{M}$, we have that $f^{-1}(S)\in\mathcal{M}$.
If $0\in S$, then $F^{-1}(S)=f^{-1}(S)\cup (X\setminus U)$. Because $f$ is measurable, we have that $f^{-1}(S)\in\mathcal{N}$, hence $f^{-1}(S)\in\mathcal{M}$. Because any $\sigma$-algebra is closed under complements and unions and $U\in\mathcal{M}$, we have that $X\setminus U\in\mathcal{M}$, and hence $f^{-1}(S)\cup (X\setminus U)\in\mathcal{M}$.
Thus $F$ is measurable.
Note that if $U$ were not measurable, this argument would not work, because (as Arturo points out below) we could not conclude that $F^{-1}(S)$ would be measurable for any $S\neq\varnothing$ or $\overline{\mathbb{R}}$.