Given a pair $(a, b) \in A \times B$, I would like to know how to write the functions which get the first element and the second element of the pair...
In a programming language, one can write $\textit{fst}: A \times B \rightarrow A$ and $\textit{fst} : (a,b) \mapsto a$. $\textit{snd}: A \times B \rightarrow B$ and $\textit{snd}:(a,b) \mapsto b$. Can we write same thing in mathematics?
Yes, those are perfectly good functions. They are called projection functions and are often (but not always) written as $\pi_1$ and $\pi_2$.
Projection functions play an important role in the definition of the primitive recursive functions of computability theory. In this context, they are often written as $P^2_1$ and $P^2_2$, where the superscript indicates the total number of arguments, and the subscript shows which of the arguments is selected.
They are also an essential component of the category-theoretic definition of products; in category theory to prove that some object $X$ is a product $A\times B$ is precisely to give its projections $\pi_A:X\to A$ and $\pi_B:X\to B$ and show that they have certain characteristic properties.